Generating Parallel Random Numbers Using Akka Futures

I am writing a processor intensive application created using Akka 2 Futures. I don’t need actors right now, but I don’t want to use them.

Several calculations in futures very often cause a random generator. I’m afraid that if I used the classic parallel RNG, it would become a strangulation point and I would lose scalability.

What is the fastest / easiest way to create a random event generator in a thread in an ExecutionContext ?

Is there a way to initialize them with another seed (but previously known) to repeat the experiment?

+6
source share
3 answers

Use akka.jsr166y.ThreadLocalRandom

+4
source

If using ThreadLocalRandom is not an option, you can easily write your own using ThreadLocal and Scala Random . Although providing “additional commonly used limited random generation methods” is an exercise left to the interested developer.

 object ThreadLocalRandom { private val localRandom = new ThreadLocal[util.Random] { override protected def initialValue() = new util.Random } def current = localRandom.get } 
+4
source

Do not use a random Scala generator; it's just a wrapper around Javas that you probably noticed is in sync.

Java 7 has a ThreadLocalRandom created for your use. If you cannot use java 7, use another randomizer; for example, the unsynchronized implementation of mersenna twister from here: http://www.cs.gmu.edu/~sean/research/ . Use this implementation through the ThreadLocal object so that each thread has one that has been initialized only once. For seed instances, just use the normal synchronized random case.

+2
source

Source: https://habr.com/ru/post/910157/


All Articles