public class Random extends Object implements Serializable
next()
and
setSeed(long)
method. In that case the above
paragraph doesn't apply to you.
This class shouldn't be used for security sensitive purposes (like
generating passwords or encryption keys. See SecureRandom
in package java.security
for this purpose.
For simple random doubles between 0.0 and 1.0, you may consider using
Math.random instead.SecureRandom
,
Math.random()
,
Serialized FormConstructor and Description |
---|
Random()
Creates a new pseudorandom number generator.
|
Random(long seed)
Creates a new pseudorandom number generator, starting with the
specified seed, using
setSeed(seed); . |
Modifier and Type | Method and Description |
---|---|
protected int |
next(int bits)
Generates the next pseudorandom number.
|
boolean |
nextBoolean()
Generates the next pseudorandom boolean.
|
void |
nextBytes(byte[] bytes)
Fills an array of bytes with random numbers.
|
double |
nextDouble()
Generates the next pseudorandom double uniformly distributed
between 0.0 (inclusive) and 1.0 (exclusive).
|
float |
nextFloat()
Generates the next pseudorandom float uniformly distributed
between 0.0f (inclusive) and 1.0f (exclusive).
|
double |
nextGaussian()
Generates the next pseudorandom, Gaussian (normally) distributed
double value, with mean 0.0 and standard deviation 1.0.
|
int |
nextInt()
Generates the next pseudorandom number.
|
int |
nextInt(int n)
Generates the next pseudorandom number.
|
long |
nextLong()
Generates the next pseudorandom long number.
|
void |
setSeed(long seed)
Sets the seed for this pseudorandom number generator.
|
public Random()
setSeed(System.currentTimeMillis());
.System.currentTimeMillis()
public Random(long seed)
setSeed(seed);
.seed
- the initial seedpublic void setSeed(long seed)
public synchronized void setSeed(long seed) { this.seed = (seed ^ 0x5DEECE66DL) & ((1L << 48) - 1); haveNextNextGaussian = false; }
seed
- the new seedprotected int next(int bits)
bits
low order bits are
independent chosen random bits (0 and 1 are equally likely).
The implementation for java.util.Random is:
protected synchronized int next(int bits) { seed = (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1); return (int) (seed >>> (48 - bits)); }
bits
- the number of random bits to generate, in the range 1..32public void nextBytes(byte[] bytes)
public void nextBytes(byte[] bytes) { for (int i = 0; i < bytes.length; i += 4) { int random = next(32); for (int j = 0; i + j < bytes.length && j < 4; j++) { bytes[i+j] = (byte) (random & 0xff) random >>= 8; } } }
bytes
- the byte array that should be filledNullPointerException
- if bytes is nullpublic int nextInt()
public int nextInt() { return next(32); }
public int nextInt(int n)
n
(exclusive), and
each value has the same likelihodd (1/n
).
(0 and 1 are equally likely). The implementation for
java.util.Random is:
public int nextInt(int n) { if (n <= 0) throw new IllegalArgumentException("n must be positive"); if ((n & -n) == n) // i.e., n is a power of 2 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; }
This algorithm would return every value with exactly the same probability, if the next()-method would be a perfect random number generator. The loop at the bottom only accepts a value, if the random number was between 0 and the highest number less then 1<<31, which is divisible by n. The probability for this is high for small n, and the worst case is 1/2 (for n=(1<<30)+1). The special treatment for n = power of 2, selects the high bits of the random number (the loop at the bottom would select the low order bits). This is done, because the low order bits of linear congruential number generators (like the one used in this class) are known to be ``less random'' than the high order bits.
n
- the upper boundIllegalArgumentException
- if the given upper bound is negativepublic long nextLong()
public long nextLong() { return ((long) next(32) << 32) + next(32); }
public boolean nextBoolean()
public boolean nextBoolean() { return next(1) != 0; }
public float nextFloat()
public float nextFloat() { return next(24) / ((float)(1 << 24)); }
public double nextDouble()
public double nextDouble() { return (((long) next(26) << 27) + next(27)) / (double)(1L << 53); }
public double nextGaussian()
public synchronized 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 is described in section 3.4.1 of The Art of Computer Programming, Volume 2 by Donald Knuth.