Quantcast
Channel: alvinalexander.com - sequence
Viewing all articles
Browse latest Browse all 25

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

$
0
0

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?

Solution

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:

read more


Viewing all articles
Browse latest Browse all 25

Latest Images



Latest Images