Normally when I seed a sequential random number generator in C, I use the call
srand(time(NULL))
then use
rand() mod N
to obtain a random integer between 0 and N-1. However, when I do this in parallel, the calls to time(NULL) are so close to each other that they end up being exactly the same number.
I have tried using a linear congruential random number generator:
$x_{n+1} := a x_n + c\;\;\; (\bmod m) $ for some integers $a,c,$ and $m$.
I know that choosing $m=2^k$ for some large integer $k$ produces fast results because the modulus operator can be calculated by truncating digits. However, I find it difficult to establish seeds that produce random sequences with a large period in parallel. I know that a period length is maximal if
- c and m are primes with respect to eachother
- a-1 is divisible by all prime factors of m
- if m is a multiple of 4, a-1 must also be a multiple of 4.
(source: wikipedia)
But how do I ensure that all random number streams have this maximal property? In terms of MPI, how do I incorporate the rank
and size
to produce maximal periods using the linear congruential method? Would it be easier to use a Lagged Fibonacci or Mersenne Twister to produce longer parallel random streams?
mod
to grab the low order bits - as Jonathan Dursi suggested, they are much less random. Instead divide your (int) random number by maxint/range to get the range you need. It costs you a divide, but it's probably a cheaper option to improve the quality of your random number stream than switching to another PRNG. $\endgroup$