nextGaussian method

double nextGaussian({
  1. double mean = 0.0,
  2. double standardDeviation = 1.0,
})

Returns a sample from a normal distribution with the given mean and standardDeviation (defaults to the standard normal N(0,1)).

Uses the Marsaglia polar method (a numerically stable Box–Muller variant).

Implementation

double nextGaussian({double mean = 0.0, double standardDeviation = 1.0}) {
  double u, v, s;
  do {
    u = nextDouble() * 2 - 1;
    v = nextDouble() * 2 - 1;
    s = u * u + v * v;
  } while (s >= 1.0 || s == 0.0);
  final multiplier = sqrt(-2.0 * log(s) / s);
  return mean + standardDeviation * (u * multiplier);
}