Random Number Seeds¶
The random number functions in most language libraries are actually pseudo-random number generators. Given the same seed, or starting value, they will reproduce the same sequence of numbers. If a relatively unique sequence is required on multiple nodes, the seeds should be generated from a source of high entropy random bits. On Linux systems, this can be accomplished by reading random bits from the system file /dev/random.
C/C++¶
The read can readily be done with fread() in C/C++ by treating /dev/random as a stream.
int main ( void )
{
#include <stdio.h>
double x;
int i[2];
FILE *fp;
fp = fopen("/dev/random","r");
fread ( &x, 1, 8, fp );
fread ( i, 1, 8, fp );
fclose ( fp );
printf ( "%i %i %g\n", i[0], i[1], x );
return 0;
}
Fortran¶
In Fortran, the size of the variable determines how many bits will be read. The /dev/random file must be treated as a binary, direct access file, with a record length corresponding to the byte size of the variable being read into.