/// <summary> /// Sample from the distribution tail (defined as having x >= __R). /// </summary> /// <returns>A new random sample from the Gaussian distribution tail.</returns> private static double SampleTail(IRandomSource rng) { double x, y; do { // Note. we use NextDoubleNonZero() because Log(0) returns -Infinity and will also tend to be a very slow execution path (when it occurs, which is rarely). x = -Math.Log(rng.NextDoubleNonZero()) / __R; y = -Math.Log(rng.NextDoubleNonZero()); }while(y + y < x * x); return(__R + x); }