Class Random
java.lang.Object
|
+----Random
- public class Random
- extends Object
- implements Parsable
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 -1 through 3 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 is written using 64-bit integers.
Algorithm 4 is included because it's based on a different concept,
and so might have different flaws than the above algorithms.
Besides, it's really neat. It is based on the algorithm in
Applied Cryptography 2nd ed. chapter 17 sect 1 (p. 397-398). It does not use
any copyrighted code, patented algorithms, or ideas that are now trade secrets.
It's called ARCFOUR here rather than dropping the A and changing FOUR to 4,
to avoid infringing on RSADSI's trademark. Since it is purely a random
number generator, it does not fall under ITAR. (And the seed is hashed
down to 40 bits anyway).
The available algorithms are:
-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 = linear congruential generators
3 = Knuth's subtractive algorithm (probably a good algorithm)
4 = ARCFOUR stream (drop A and change FOUR to 4 for another name for it)
The default algorithm is number 1.
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.
Algorithm 4 has been analyzed since 1987 by a few people, and since 1994
by many more, without many flaws being discovered, so it is
probably a good algorithm for random number generation. The stream of
random 64-bit numbers it produces will repeat in less than about
10^450 iterations. It might repeat much faster than that, but no one
has ever proved the existence of much shorter cycles, so it may really
last about that long.
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.3, 10 Sep 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
-
Random(long, int)
- creates a new random number generator and sets its seed and algorithm (in [-1..4])
-
BNF(int)
-
-
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.
-
getParameters(int)
- Return a parameter array if BNF(), parse(), and unparse() are to be automated, null otherwise.
-
initialize(int)
- Initialize, either partially or completely.
-
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.
-
parse(Parser, int)
- Parse the input file to get the parameters for this object.
-
setSeed()
- Sets the random number generator seed from the system clock
-
setSeed(long)
- Sets the random number generator seed
-
setSeedAlgorithm(long, int)
- Sets which algorithm to use henceforth.
-
unparse(Unparser, int)
- Output a description of this object that can be parsed with parse().
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
Random
public Random(long s,
int alg)
- creates a new random number generator and sets its seed and algorithm (in [-1..4])
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.
setSeedAlgorithm
public final void setSeedAlgorithm(long s,
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.
-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 = linear congruential generators
3 = Knuth's subtractive algorithm (probably a good algorithm)
4 = ARCFOUR (40-bit seed, outputs 56-bit longs, drop A and change FOUR to 4 to get another name for this 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.
getParameters
public Object[][] getParameters(int lang)
- Return a parameter array if BNF(), parse(), and unparse() are to be automated, null otherwise.
- See Also:
- getParameters
BNF
public String BNF(int lang)
unparse
public void unparse(Unparser u,
int lang)
- Output a description of this object that can be parsed with parse().
parse
public Object parse(Parser p,
int lang) throws ParserException
- Parse the input file to get the parameters for this object.
- Throws: ParserException
- parser didn't find the required token
initialize
public void initialize(int level)
- Initialize, either partially or completely.
- See Also:
- initialize