scala random number
To generate a random number in Scala, you can use the scala.util.Random
class. Here's an example:
import scala.util.Random
val random = new Random()
val randomNumber = random.nextInt()
In this example, we import the scala.util.Random
class and create an instance of it called random
. Then, we use the nextInt()
method to generate a random integer.
If you want to generate a random number within a specific range, you can use the nextInt(n: Int)
method, where n
is the upper bound (exclusive). For example, to generate a random number between 0 and 9, you can do:
val randomNumberInRange = random.nextInt(10)
This will generate a random number between 0 and 9.
Please note that the scala.util.Random
class uses a default random number generator, which may not be suitable for cryptographic purposes. If you need a cryptographically secure random number generator, you should consider using a different library or approach.
I hope this helps!