mercredi 10 février 2021

Is the random generator in Scala thread-safe [duplicate]

I have the following code:

import scala.util.Random

val seq = Seq[Int](1, 2, 3)
val rand = new Random(123)
val gen = seq.par.map(_ => rand.nextInt(3))
println(gen.seq)

It prints out Vector(2, 2, 2).
It seems that the random generator in Scala is not thread-safe?
Is that true? If so, how I could tackle with it on top of using different seeds for different threads.
Also, if I use the random class in Java in Scala, will that still be thread-safe?
Related Question


Explanation
par refers to the par in scala.collection.parallel.immutable. But it seems that my computer doesn't require me to do that. And it runs on my computer.




Aucun commentaire:

Enregistrer un commentaire