The implementation of nextGaussian in Java 8 is as follows:
The function used to generate next values has extremes when both v1 and v2 are as close to zero as possible, without being both equal to zero.private double nextNextGaussian; private boolean haveNextNextGaussian = false; public double nextGaussian() { if (haveNextNextGaussian) { haveNextNextGaussian = false; return nextNextGaussian; } else { double v1, v2, s; do { v1 = 2 * nextDouble() - 1; // between -1.0 and 1.0 v2 = 2 * nextDouble() - 1; // between -1.0 and 1.0 s = v1 * v1 + v2 * v2; } while (s >= 1 || s == 0); double multiplier = StrictMath.sqrt(-2 * StrictMath.log(s)/s); nextNextGaussian = v2 * multiplier; haveNextNextGaussian = true; return v1 * multiplier; } }
Quick check of nextDouble indicates that the function generates only multiples of
1 / (double)(1L << 53)
. This means that nextGaussian will return extreme values for v1 = +/- 2 / (double)(1L << 53)
and v2 = 0.For these values nextGaussian would return +/- 12.00727336061225.
That does not mean that it is possible to get these values. There's a limited number of values that can be returned by nextDouble. But it does mean that nextGaussian (in its Oracle Java 8 implementation) will never return a value outside of the range between -12.00727336061225 and 12.00727336061225.
No comments:
Post a Comment