Random.NextGaussian Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Returns the next pseudorandom, Gaussian ("normally") distributed
double
value with mean 0.0
and standard
deviation 1.0
from this random number generator's sequence.
[Android.Runtime.Register("nextGaussian", "()D", "GetNextGaussianHandler")]
public virtual double NextGaussian ();
[<Android.Runtime.Register("nextGaussian", "()D", "GetNextGaussianHandler")>]
abstract member NextGaussian : unit -> double
override this.NextGaussian : unit -> double
Returns
the next pseudorandom, Gaussian ("normally") distributed
double
value with mean 0.0
and
standard deviation 1.0
from this random number
generator's sequence
- Attributes
Remarks
Returns the next pseudorandom, Gaussian ("normally") distributed double
value with mean 0.0
and standard deviation 1.0
from this random number generator's sequence.
The general contract of nextGaussian
is that one double
value, chosen from (approximately) the usual normal distribution with mean 0.0
and standard deviation 1.0
, is pseudorandomly generated and returned.
The method nextGaussian
is implemented by class Random
as if by a threadsafe version of the following:
{@code
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;
}
}}
This uses the polar method of G. E. P. Box, M. E. Muller, and G. Marsaglia, as described by Donald E. Knuth in The Art of Computer Programming, Volume 2: Seminumerical Algorithms, section 3.4.1, subsection C, algorithm P. Note that it generates two independent values at the cost of only one call to StrictMath.log
and one call to StrictMath.sqrt
.
Java documentation for java.util.Random.nextGaussian()
.
Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.