How to generate random numbers, characters, and sequences in Scala

Scala FAQ: How do I generate random numbers (or characters) in Scala, such as when testing an application, performing a simulation, and many other situations?

How to generate random numbers in Scala

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

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 Scala 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.

How to generate random characters

You can also generate random characters in Scala:

// random characters
scala> r.nextPrintableChar
res0: Char = H

scala> r.nextPrintableChar
res1: Char = r

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 section.

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)

How to generate a random range/list/sequence

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)

Creating a random-length list/array/sequence

You can easily create random-length ranges of other types. Here’s a random-length collection of up to ten 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)

How to create a sequence with random values

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)

See Also