Fast multithreaded random sequence in C ++

I need to use random in a multi-threaded loop, but for each loop, no matter which thread evaluates that loop, require the same values ​​through each loop.

Without multithreading, I can do:

seed(1)
for (unsigned int i=0; i<100; i++){
     rand()
}

      

In the loop, every time rand () is called, it will return a new pseudo-random value. Now I want to create the same list with multiple threads.

The main reason is that I want the same sequence of pseudo-random numbers, independent of the order of the loop (which is more like an "i" inside the loop).

Note. I went through a lot of "random" and "multithreaded" topics but found solutions on how to make sure the numbers are always random and not the other way around.

EDIT:

The result should be as if a random list were generated from the seed. And this random list must be used in a loop with index "i" to get a pseudo-random value. So in the loop, the same random number will be at index "i" based on the seed. (However, there should not be one random number the same throughout the loop, but the sequence must be the same for each run of the program. Regardless of the number of threads or which thread evaluates part of the loop.)

It would not be very efficient to create this list (without multithreading) and use this initialized list in a multithreaded loop. Any thoughts on this?

+3


source to share


2 answers


Use any non-STDC random number generator with a separate managed state. I recommend this: http://www.boost.org/doc/libs/1_52_0/doc/html/boost_random.html



+4


source


You want a counter based random generator, not a congruent one. A good document explaining how they work is here:



http://www.thesalmons.org/john/random123/papers/random123sc11.pdf

+3


source







All Articles