Back to Supermpi

Random number initialisation

Pseudorandom number generators need to be initialised, by setting their seed. This seed should be different for each  node your multi-process program runs on, otherwise subtle statistical effects may destroy your results.

The usual way to initialise a PRNG is to obtain the current time. This is insufficient for a parallel job, because the clocks on each of the nodes may be synchronised. Instead, the best thing to do is to use the /dev/urandom device, which is available on Un*x operating systems. This device produces cryptographically strong random numbers, based on random data such as packet arrival times.

Following is a code sample demonstrating this method.

FILE * devRandom = fopen("/dev/urandom", "r");
if (devRandom) {
fread(&idum, 4, 1, devRandom);
idum = -abs((int)idum);
#ifdef DEBUG2
printf("Random number seed %d\n", idum);
#endif
fclose(devRandom);
} else {
printf("Warning: could not open /dev/urandom\n");
idum = -((time(NULL) ^ mpiRank) % 1000000000);
}
If you are using a system which does not have a /dev/urandom device, it may be sufficient to seed the random number generator with some combination of the MPI rank, and the starting time. An example of this is shown above, in the "else" section.