Class Random
java.lang.Object
|
+----Random
- public class Random
- extends Object
Random number generator. This is a plug-in replacement for the
built-in generator in Java. This is used instead to ensure
it is repeatable, won't change in future versions of Java, and
has known good security. The algorithms used come from Numerical
Recipies in C (2nd ed), but the actual code is original, and so
not subject to their copyright. The code here is often simpler
than the C code, since it can be written using 64-bit integers.
This code is (c) 1996,1997 Leemon Baird
<leemon@cs.cmu.edu>,
http://www.cs.cmu.edu/~baird
The source and object code may be redistributed freely.
If the code is modified, please state so in the comments.
- Version:
- 1.1, 22 July 97
- Author:
- Leemon Baird
-
Random()
- creates a new random number generator and sets its seed
randomly according to the current time.
-
Random(long)
- creates a new random number generator and sets its seed
-
clone()
- Create a copy of this generator which will generate the same sequence.
-
copyInto(Random)
- Make the existing Random rnd into an exact clone of this Random.
-
maxLong()
- return the maximum value ever returned by nextLong()
-
minLong()
- return the minimum value ever returned by nextLong()
-
nextDouble()
- Generates a pseudorandom, uniformly-distributed, double value
in the range [0.0,1.0)
-
nextFloat()
- Generates a pseudorandom, uniformly-distributed, float value
in the range [0.0,1.0)
-
nextInt()
- Generates a pseudorandom, uniformly-distributed, int value.
-
nextInt(int, int)
- Generates a pseudorandom, uniformly-distributed, int value in [min,max].
-
nextLong()
- Generates a pseudorandom, uniformly-distributed, long value.
-
setAlgorithm(int)
- Sets which algorithm to use henceforth.
-
setSeed()
- Sets the random number generator seed from the system clock
-
setSeed(long)
- Sets the random number generator seed
Random
public Random()
- creates a new random number generator and sets its seed
randomly according to the current time.
Random
public Random(long s)
- creates a new random number generator and sets its seed
clone
public Object clone()
- Create a copy of this generator which will generate the same sequence.
- Overrides:
- clone in class Object
copyInto
public void copyInto(Random rnd)
- Make the existing Random rnd into an exact clone of this Random.
It will then generate the same sequence as this.
setAlgorithm
public final void setAlgorithm(int alg)
- Sets which algorithm to use henceforth.
Compared to algorithm zero, 1 takes 30% longer to run, 2 takes
twice as long, and 3 takes 40% less time. Numerical Recipes
in C recommends using 1 in most cases, or 2 for extremely good numbers,
and offers a $1000 prize for anyone finding flaws in 2.
If more than 100 million numbers are needed, it recommends
2 rather than 1, even though it takes 50% longer.
Note that for 1, 2, and 3, if the random number generator is
seeded with a given number and then run, it will always generate
the same sequence. But, if one looks at the seed halfway through
the sequence, reseeding with that seed in the future will *not*
make it generate the second half of the sequence. This is because
the true "seed" is actually a large table which is regenerated
from scratch every time setSeed() is called.
To store the state of a sequence so it can be regenerated later,
use clone().
-1 = ANSI C "example" algorithm (is bad, and only creates 16-bit rnd #s)
0 = Park and Miller Minimum Standard (CACM, 88, from Lewis,Goodman,Miller, 69)
1 = alg 0 plus random shuffling
2 = two LCGs combined with a shuffle
3 = Knuth's subtractive algorithm (probably a good algorithm)
setSeed
public final void setSeed(long s)
- Sets the random number generator seed
setSeed
public final void setSeed()
- Sets the random number generator seed from the system clock
minLong
public final long minLong()
- return the minimum value ever returned by nextLong()
maxLong
public final long maxLong()
- return the maximum value ever returned by nextLong()
nextLong
public final long nextLong()
- Generates a pseudorandom, uniformly-distributed, long value.
The number is between minLong() and maxLong() inclusive, which
depends on which algorithm is currently selected.
nextDouble
public final double nextDouble()
- Generates a pseudorandom, uniformly-distributed, double value
in the range [0.0,1.0)
nextFloat
public final float nextFloat()
- Generates a pseudorandom, uniformly-distributed, float value
in the range [0.0,1.0)
nextInt
public final int nextInt(int min,
int max)
- Generates a pseudorandom, uniformly-distributed, int value in [min,max].
nextInt
public final int nextInt()
- Generates a pseudorandom, uniformly-distributed, int value.