Class JVMRandom
- All Implemented Interfaces:
Serializable
JVMRandom is a wrapper that supports all possible
Random methods via the Math.random() method
and its system-wide Random object.
It does this to allow for a Random class in which the seed is shared between all members of the class - a better name would have been SharedSeedRandom.
N.B. the current implementation overrides the methods
Random.nextInt(int) and Random.nextLong()
to produce positive numbers ranging from 0 (inclusive)
to MAX_VALUE (exclusive).
- Since:
- 2.0
- Version:
- $Id: JVMRandom.java 911986 2010-02-19 21:19:05Z niallp $
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate booleanEnsures that only the parent constructor can call reseed.private static final longRequired for serialization support.private static final Random -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static intbitsRequired(long num) Count the number of bits required to represent a long number.private static longGet the next unsigned random longbooleanReturns the next pseudorandom, uniformly distributed boolean value from the Math.random() sequence.voidnextBytes(byte[] byteArray) Unsupported in 2.0.doubleSynonymous to the Math.random() call.floatReturns the next pseudorandom, uniformly distributed float value between0.0and1.0from the Math.random() sequence.doubleUnsupported in 2.0.intnextInt()Returns the next pseudorandom, uniformly distributed int value from the Math.random() sequence.intnextInt(int n) Returns a pseudorandom, uniformly distributed int value between0(inclusive) and the specified value (exclusive), from the Math.random() sequence.longnextLong()Returns the next pseudorandom, uniformly distributed long value from the Math.random() sequence.static longnextLong(long n) Returns a pseudorandom, uniformly distributed long value between0(inclusive) and the specified value (exclusive), from the Math.random() sequence.voidsetSeed(long seed) Unsupported in 2.0.
-
Field Details
-
serialVersionUID
private static final long serialVersionUIDRequired for serialization support.- See Also:
-
SHARED_RANDOM
-
constructed
private boolean constructedEnsures that only the parent constructor can call reseed.
-
-
Constructor Details
-
JVMRandom
public JVMRandom()Constructs a new instance.
-
-
Method Details
-
setSeed
public void setSeed(long seed) Unsupported in 2.0.- Overrides:
setSeedin classRandom- Parameters:
seed- ignored- Throws:
UnsupportedOperationException
-
nextGaussian
public double nextGaussian()Unsupported in 2.0.- Overrides:
nextGaussianin classRandom- Returns:
- Nothing, this method always throws an UnsupportedOperationException.
- Throws:
UnsupportedOperationException
-
nextBytes
public void nextBytes(byte[] byteArray) Unsupported in 2.0.- Overrides:
nextBytesin classRandom- Parameters:
byteArray- ignored- Throws:
UnsupportedOperationException
-
nextInt
public int nextInt()Returns the next pseudorandom, uniformly distributed int value from the Math.random() sequence.
Identical tonextInt(Integer.MAX_VALUE)N.B. All values are >= 0.
-
nextInt
public int nextInt(int n) Returns a pseudorandom, uniformly distributed int value between
0(inclusive) and the specified value (exclusive), from the Math.random() sequence.- Overrides:
nextIntin classRandom- Parameters:
n- the specified exclusive max-value- Returns:
- the random int
- Throws:
IllegalArgumentException- whenn <= 0
-
nextLong
public long nextLong()Returns the next pseudorandom, uniformly distributed long value from the Math.random() sequence.
Identical tonextLong(Long.MAX_VALUE)N.B. All values are >= 0.
-
nextLong
public static long nextLong(long n) Returns a pseudorandom, uniformly distributed long value between
0(inclusive) and the specified value (exclusive), from the Math.random() sequence.- Parameters:
n- the specified exclusive max-value- Returns:
- the random long
- Throws:
IllegalArgumentException- whenn <= 0
-
nextBoolean
public boolean nextBoolean()Returns the next pseudorandom, uniformly distributed boolean value from the Math.random() sequence.
- Overrides:
nextBooleanin classRandom- Returns:
- the random boolean
-
nextFloat
public float nextFloat()Returns the next pseudorandom, uniformly distributed float value between
0.0and1.0from the Math.random() sequence. -
nextDouble
public double nextDouble()Synonymous to the Math.random() call.
- Overrides:
nextDoublein classRandom- Returns:
- the random double
-
next63bits
private static long next63bits()Get the next unsigned random long- Returns:
- unsigned random long
-
bitsRequired
private static int bitsRequired(long num) Count the number of bits required to represent a long number.- Parameters:
num- long number- Returns:
- number of bits required
-