normal method

double normal()

Calculate a random number with a normal distribution.

Note that this means results may be less than -1.0 or greater than 1.0.

Uses https://en.wikipedia.org/wiki/Marsaglia_polar_method.

Implementation

double normal() {
  double u, v, lengthSquared;

  do {
    u = rng.float(-1.0, 1.0);
    v = rng.float(-1.0, 1.0);
    lengthSquared = u * u + v * v;
  } while (lengthSquared >= 1.0);

  return u * math.sqrt(-2.0 * math.log(lengthSquared) / lengthSquared);
}