Scala – generate a list of random numbers

Using the scala fill operator, you can easily create a large list, and then populate that list with random entries:

val z = List.fill(1000)(10000).map(scala.util.Random.nextInt)

This will generate a list of 1,000 entries, each containing the number 10,000, which is used to generate a random number for that list entry (i.e. 0 to 10,000):

z: List[Int] = List(19436, 73559, 96382, 22910, 47398, 83820, 1630, 95466, 44891, 40060, 52702, 89086, 52120, 55565, 44399, 39384, 72965, 26512, 23219, 63453, 990, 37886, 2994, 86494, 41212, 46286, 65486, 44614, 35539, 72433, 9710, 20899, 62996, 12970, 75063, 61006, 3763, 44941, 89145, 32184, 56227, 61843, 93381, 3603, 75101, 81463, 47837, 61211, 86623, 38833, 83587, 287, 29393, 58860, 24595, 50864, 59747, 95237, 95245, 67748, 72658, 51490, 72874, 2855, 49481, 25293, 55248, 25664, 92292, 34892, 16992, 71963, 46768, 91903, 81261, 58776, 26798, 9205, 47431, 18671, 53079, 17322, 55733, 1277, 44473, 21537, 22465, 14512, 42176, 94234, 2286, 39820, 94704, 98961, 46788, 61623, 40974, 21683, 71372, 41895, 76286, 23357, 52151, 40870, 68518, 23155, 37816, 93887, 16082, 38841, 45650, 73544, 2289, ...

Leave a Reply

Your email address will not be published. Required fields are marked *