Generating Random Numbers
You need to create random numbers, such as when testing an application, performing a simulation, and many other situations.
Create random numbers with the Scala scala.util.Random class. You can create random integers:
scala> val r = scala.util.Random
r: scala.util.Random = scala.util.Random@13eb41e5
scala> r.nextInt
res0: Int = −1323477914
You can limit the random numbers to a maximum value:
scala> r.nextInt(100)
res1: Int = 58
In this use, the Int returned is between 0 (inclusive) and the value you specify (exclusive), so specifying 100 returns an Int from 0 to 99 .
You can also create random Float values:
// returns a value between 0.0 and 1.0
scala> r.nextFloat
res2: Float = 0.50317204
You can create random Double values:
// returns a value between 0.0 and 1.0
scala> r.nextDouble
res3: Double = 0.6946000981900997
You can set the seed value using an Int or Long when creating the Random object:
scala> val r = new scala.util.Random(100)
r: scala.util.Random = scala.util.Random@bbf4061
You can also set the seed value after a Random object has been created:
r.setSeed(1000L)
The Random class handles all the usual use cases, including creating numbers, setting the maximum value of a random number range, and setting a seed value. You can also generate random characters:
// random characters
scala> r.nextPrintableChar
res0: Char = H
scala> r.nextPrintableChar
res1: Char = r
Scala makes it easy to create a random-length range of numbers, which is especially useful for testing:
// create a random length range
scala> var range = 0 to r.nextInt(10)
range: scala.collection.immutable.Range.Inclusive = Range(0, 1, 2, 3)
scala> range = 0 to r.nextInt(10)
range: scala.collection.immutable.Range.Inclusive = Range(0, 1)
You can add a for / yield loop to modify the numbers:
scala> for (i <- 0 to r.nextInt(10)) yield i * 2
res0: scala.collection.immutable.IndexedSeq[Int] = Vector(0, 2, 4)
You can easily create random-length ranges of other types. Here’s a random-length collection of up to 10 Float values:
scala> for (i <- 0 to r.nextInt(10)) yield (i * r.nextFloat)
res1: scala.collection.immutable.IndexedSeq[Float] =
Vector(0.0, 0.71370363, 1.0783684)
Here’s a random-length collection of “printable characters”:
scala> for (i <- 0 to r.nextInt(10)) yield r.nextPrintableChar
res2: scala.collection.immutable.IndexedSeq[Char] = Vector(x, K, ^, z, w)
Be careful with the nextPrintableChar method. A better approach may be to control the characters you use, as shown in my “How to create a list of alpha or alphanumeric characters” article, shown in the See Also.
Conversely, you can create a sequence of known length, filled with random numbers:
scala> for (i <- 1 to 5) yield r.nextInt(100)
res3: scala.collection.immutable.IndexedSeq[Int] = Vector(88, 94, 58, 96, 82)
You need to create random numbers, such as when testing an application, performing a simulation, and many other situations.
Create random numbers with the Scala scala.util.Random class. You can create random integers:
scala> val r = scala.util.Random
r: scala.util.Random = scala.util.Random@13eb41e5
scala> r.nextInt
res0: Int = −1323477914
You can limit the random numbers to a maximum value:
scala> r.nextInt(100)
res1: Int = 58
In this use, the Int returned is between 0 (inclusive) and the value you specify (exclusive), so specifying 100 returns an Int from 0 to 99 .
You can also create random Float values:
// returns a value between 0.0 and 1.0
scala> r.nextFloat
res2: Float = 0.50317204
You can create random Double values:
// returns a value between 0.0 and 1.0
scala> r.nextDouble
res3: Double = 0.6946000981900997
You can set the seed value using an Int or Long when creating the Random object:
scala> val r = new scala.util.Random(100)
r: scala.util.Random = scala.util.Random@bbf4061
You can also set the seed value after a Random object has been created:
r.setSeed(1000L)
The Random class handles all the usual use cases, including creating numbers, setting the maximum value of a random number range, and setting a seed value. You can also generate random characters:
// random characters
scala> r.nextPrintableChar
res0: Char = H
scala> r.nextPrintableChar
res1: Char = r
Scala makes it easy to create a random-length range of numbers, which is especially useful for testing:
// create a random length range
scala> var range = 0 to r.nextInt(10)
range: scala.collection.immutable.Range.Inclusive = Range(0, 1, 2, 3)
scala> range = 0 to r.nextInt(10)
range: scala.collection.immutable.Range.Inclusive = Range(0, 1)
You can add a for / yield loop to modify the numbers:
scala> for (i <- 0 to r.nextInt(10)) yield i * 2
res0: scala.collection.immutable.IndexedSeq[Int] = Vector(0, 2, 4)
You can easily create random-length ranges of other types. Here’s a random-length collection of up to 10 Float values:
scala> for (i <- 0 to r.nextInt(10)) yield (i * r.nextFloat)
res1: scala.collection.immutable.IndexedSeq[Float] =
Vector(0.0, 0.71370363, 1.0783684)
Here’s a random-length collection of “printable characters”:
scala> for (i <- 0 to r.nextInt(10)) yield r.nextPrintableChar
res2: scala.collection.immutable.IndexedSeq[Char] = Vector(x, K, ^, z, w)
Be careful with the nextPrintableChar method. A better approach may be to control the characters you use, as shown in my “How to create a list of alpha or alphanumeric characters” article, shown in the See Also.
Conversely, you can create a sequence of known length, filled with random numbers:
scala> for (i <- 1 to 5) yield r.nextInt(100)
res3: scala.collection.immutable.IndexedSeq[Int] = Vector(88, 94, 58, 96, 82)
Share this article with your friends.
No comments :
Post a Comment