grand function

double grand (
  1. Random rand
)

Return a random variable following a gaussian distribution and a standard deviation of 1.

Implementation

double grand(Random rand) {
  double x1, w;
  do {
    double x2 = 2.0 * rand.nextDouble() - 1.0;
    x1 = 2.0 * rand.nextDouble() - 1.0;
    w = x1 * x1 + x2 * x2;
  } while (w <= 0.0 || w >= 1.0);

  return x1 * sqrt((-2.0 * log(w)) / w);
}