Random Number Generation (RAND)


RAND1, RANDG
NAME
    rand1 - Generate uniform random number within the range of [-1., +1.]
    randg - Generate Gaussian random number with zero mean and dispersion 1.

SYNOPSIS

      rv1 = rand1()
  rv1   [float]  Result (Uniform random number generated).

      rvg = randg()
  rvg   [float]  Result (Gaussian random number generated).

SOURCE LIST (in C language)

  #include <stdlib.h>
  #include <math.h>
  double rand1(void) { return(drand48()*2.-1.); }
  double randg(void)      /* after Box-Muller method */
  {   static int k=0;
      static double u, v, r, f;
      while (k == 0) {
          u = rand1(); v = rand1(); r = u*u+v*v;
          if ((r<1.) && (r>0.))
              { f = sqrt(-2.*log(r)/r); k = 1; return(u*f); }
      }
      k = 0; return(v*f);
  }
  float rand1_(void) { return((float)rand1()); }
  float randg_(void) { return((float)randg()); }