Methods Summary |
---|
protected synchronized int | next(int bits)Returns a pseudo-random uniformly distributed {@code int} value of
the number of bits specified by the argument {@code bits} as
described by Donald E. Knuth in The Art of Computer Programming,
Volume 2: Seminumerical Algorithms, section 3.2.1.
seed = (seed * multiplier + 0xbL) & ((1L << 48) - 1);
return (int) (seed >>> (48 - bits));
|
public boolean | nextBoolean()Returns the next pseudo-random, uniformly distributed {@code boolean} value
generated by this generator.
return next(1) != 0;
|
public void | nextBytes(byte[] buf)Modifies the {@code byte} array by a random sequence of {@code byte}s generated by this
random number generator.
int rand = 0, count = 0, loop = 0;
while (count < buf.length) {
if (loop == 0) {
rand = nextInt();
loop = 3;
} else {
loop--;
}
buf[count++] = (byte) rand;
rand >>= 8;
}
|
public double | nextDouble()Generates a normally distributed random {@code double} number between 0.0
inclusively and 1.0 exclusively.
return ((((long) next(26) << 27) + next(27)) / (double) (1L << 53));
|
public float | nextFloat()Generates a normally distributed random {@code float} number between 0.0
inclusively and 1.0 exclusively.
return (next(24) / 16777216f);
|
public synchronized double | nextGaussian()Pseudo-randomly generates (approximately) a normally distributed
{@code double} value with mean 0.0 and a standard deviation value
of {@code 1.0} using 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.
if (haveNextNextGaussian) { // if X1 has been returned, return the
// second Gaussian
haveNextNextGaussian = false;
return nextNextGaussian;
}
double v1, v2, s;
do {
v1 = 2 * nextDouble() - 1; // Generates two independent random
// variables U1, U2
v2 = 2 * nextDouble() - 1;
s = v1 * v1 + v2 * v2;
} while (s >= 1);
double norm = Math.sqrt(-2 * Math.log(s) / s);
nextNextGaussian = v2 * norm; // should that not be norm instead
// of multiplier ?
haveNextNextGaussian = true;
return v1 * norm; // should that not be norm instead of multiplier
// ?
|
public int | nextInt(int n)Returns a new pseudo-random {@code int} value which is uniformly distributed
between 0 (inclusively) and the value of {@code n} (exclusively).
if (n > 0) {
if ((n & -n) == n) {
return (int) ((n * (long) next(31)) >> 31);
}
int bits, val;
do {
bits = next(31);
val = bits % n;
} while (bits - val + (n - 1) < 0);
return val;
}
throw new IllegalArgumentException();
|
public int | nextInt()Generates a uniformly distributed 32-bit {@code int} value from
the random number sequence.
return next(32);
|
public long | nextLong()Generates a uniformly distributed 64-bit integer value from
the random number sequence.
return ((long) next(32) << 32) + next(32);
|
public synchronized void | setSeed(long seed)Modifies the seed a using linear congruential formula presented in The
Art of Computer Programming, Volume 2, Section 3.2.1.
this.seed = (seed ^ multiplier) & ((1L << 48) - 1);
haveNextNextGaussian = false;
|