21.9
java.util.Random
THE PACKAGE JAVA.UTIL
return (((long)next(27) << 27) + next(27))
/ (double)(1L << 54);
This might seem to be equivalent, if not better, but in fact it introduced a large
nonuniformity because of the bias in the rounding of floating point numbers: it
was three times as likely that the low order bit of the significand would be
0
than
that it would be
1
! This nonuniformity probably doesn't matter much in practice,
but we strive for perfection.]
21.9.12
public double nextGaussian()
The general contract of
nextGaussian
is that one
double
value, chosen from
(approximately) the usual normal distribution with mean
0.0
and standard devia
tion
1.0
, is pseudorandomly generated and returned.
The method
setSeed
is implemented by class
Random
as follows:
synchronized 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);
double norm = Math.sqrt( 2 * Math.log(s)/s);
nextNextGaussian = v2 * norm;
haveNextNextGaussian = true;
return v1 * norm;
}
}
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
Math.log
and one
call to
Math.sqrt
.
. . . who can tell what may be the event? . . .
The mind of the multitude is left at random . . .
Thomas Paine,
Common Sense
(1776), Appendix A
650
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved