J. C. Leitão J. C. Leitão - 4 months ago 43
C Question

How to properly choose rng seed for parallel processes

I'm currently working on a C/C++ project where I'm using a random number generator (gsl or boost). The whole idea can be simplified to a non-trivial stochastic process which receives a seed and returns results. I'm computing averages over different realisations of the process.

So, the seed is important: the processes must be with different seeds or it will bias the averages.

So far, I'm using

to give a seed. However, if two processes start at the same second, the seed is the same. That happens because I'm using parallelisation (using openMP).

So, my question is: how to implement a "seed giver" on C/C++ which gives independent seeds?

For instance, I though in using the thread number (
seed = time(NULL)*thread_num
. However, this means that the seeds are correlated: they are multiple of each others. Does that poses any problem to the "pseudo-random" or is it as good as sequential seeds?

The requirements are that it must work on both Mac OS (my pc) and Linux distribution similar to OS Cent (the cluster) (and naturally give independent realisations).


We ran into a similar problem on a Beowulf computing grid, the solution we used was to incorporate the pid of the process into the RNG seed, like so:


Of course, you could just read from /dev/urandom or /dev/random into an integer.