sample property

double get sample

Implementation

double get sample {
  if (_hasGauss) {
    final double tmp = _gauss;
    _gauss = 0.0;
    _hasGauss = false;
    return tmp;
  } else {
    double x1, x2, r2;

    do {
      x1 = 2.0 * _uniform.nextDouble() - 1.0;
      x2 = 2.0 * _uniform.nextDouble() - 1.0;
      r2 = x1 * x1 + x2 * x2;
    } while (r2 >= 1.0 || r2 == 0.0);

    // Box-Muller transform
    final double f = sqrt(-2.0 * log(r2) / r2);
    // Keep for next call
    _gauss = f * x1;
    _hasGauss = true;
    return f * x2;
  }
}