Random number generation

Experimental html version of downloadable textbook, see http://www.tacc.utexas.edu/~eijkhout/istc/istc.html
\[ \newcommand\inv{^{-1}}\newcommand\invt{^{-t}} \newcommand\bbP{\mathbb{P}} \newcommand\bbR{\mathbb{R}} \newcommand\defined{ \mathrel{\lower 5pt \hbox{${\equiv\atop\mathrm{\scriptstyle D}}$}}} \] Back to Table of Contents

46 Random number generation

Here is how you initialize the random number generator uniquely on each process:

{\footnotesize

C:


// Initialize the random number generator
srand((int)(mytid*(double)RAND_MAX/ntids));
// compute a random float between [0,1]
randomfraction = (rand() / (double)RAND_MAX);
// compute random integer between [0,N-1]
randomfraction = rand() % N;
Fortran:


  integer :: randsize
  integer,allocatable,dimension(:) :: randseed
  real :: random_value


  call random_seed(size=randsize)
  allocate(randseed(randsize))
  randseed(:) = 1023*mytid
  call random_seed(put=randseed)

}

Back to Table of Contents