Package org.apache.commons.math3.random
Class ISAACRandom
java.lang.Object
org.apache.commons.math3.random.BitsStreamGenerator
org.apache.commons.math3.random.ISAACRandom
- All Implemented Interfaces:
Serializable
,RandomGenerator
ISAAC: a fast cryptographic pseudo-random number generator
ISAAC (Indirection, Shift, Accumulate, Add, and Count) generates 32-bit random numbers. ISAAC has been designed to be cryptographically secure and is inspired by RC4. Cycles are guaranteed to be at least 240 values long, and they are 28295 values long on average. The results are uniformly distributed, unbiased, and unpredictable unless you know the seed.
This code is based (with minor changes and improvements) on the original implementation of the algorithm by Bob Jenkins.
ISAAC (Indirection, Shift, Accumulate, Add, and Count) generates 32-bit random numbers. ISAAC has been designed to be cryptographically secure and is inspired by RC4. Cycles are guaranteed to be at least 240 values long, and they are 28295 values long on average. The results are uniformly distributed, unbiased, and unpredictable unless you know the seed.
This code is based (with minor changes and improvements) on the original implementation of the algorithm by Bob Jenkins.
- Since:
- 3.0
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final int[]
Service variable.private int
Count through the results in rsl[]private static final int
The golden ratioprivate static final int
Half-size of rsl[] and mem[]private int
Accumulatorprivate int
The last resultprivate int
Counter, guarantees cycle is at least 2^40private int
Service variable.private int
Service variable.private int
Service variable.private static final int
For pseudo-random lookupprivate final int[]
The internal stateprivate final int[]
The results given to the userprivate static final long
Serializable version identifierprivate static final int
Size of rsl[] and mem[]private static final int
Log of size of rsl[] and mem[] -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new ISAAC random number generator.ISAACRandom
(int[] seed) Creates a new ISAAC random number generator using an int array seed.ISAACRandom
(long seed) Creates a new ISAAC random number generator using a single long seed. -
Method Summary
Modifier and TypeMethodDescriptionprivate void
Initialize, or reinitialize, this instance of rand.private void
isaac()
Generate 256 resultsprivate void
isaac2()
Intermediate internal loop.private void
isaac3()
Lowest level internal loop.protected int
next
(int bits) Generate next pseudorandom number.void
setSeed
(int seed) Sets the seed of the underlying random number generator using anint
seed.void
setSeed
(int[] seed) Sets the seed of the underlying random number generator using anint
array seed.void
setSeed
(long seed) Sets the seed of the underlying random number generator using along
seed.private void
setState
(int start) Set the state by copying the internal arrays.private void
shuffle()
Shuffle array.Methods inherited from class org.apache.commons.math3.random.BitsStreamGenerator
clear, nextBoolean, nextBytes, nextBytes, nextDouble, nextFloat, nextGaussian, nextInt, nextInt, nextLong, nextLong
-
Field Details
-
serialVersionUID
private static final long serialVersionUIDSerializable version identifier- See Also:
-
SIZE_L
private static final int SIZE_LLog of size of rsl[] and mem[]- See Also:
-
SIZE
private static final int SIZESize of rsl[] and mem[]- See Also:
-
H_SIZE
private static final int H_SIZEHalf-size of rsl[] and mem[]- See Also:
-
MASK
private static final int MASKFor pseudo-random lookup- See Also:
-
GLD_RATIO
private static final int GLD_RATIOThe golden ratio- See Also:
-
rsl
private final int[] rslThe results given to the user -
mem
private final int[] memThe internal state -
count
private int countCount through the results in rsl[] -
isaacA
private int isaacAAccumulator -
isaacB
private int isaacBThe last result -
isaacC
private int isaacCCounter, guarantees cycle is at least 2^40 -
arr
private final int[] arrService variable. -
isaacX
private int isaacXService variable. -
isaacI
private int isaacIService variable. -
isaacJ
private int isaacJService variable.
-
-
Constructor Details
-
ISAACRandom
public ISAACRandom()Creates a new ISAAC random number generator.
The instance is initialized using a combination of the current time and system hash code of the instance as the seed. -
ISAACRandom
public ISAACRandom(long seed) Creates a new ISAAC random number generator using a single long seed.- Parameters:
seed
- Initial seed.
-
ISAACRandom
public ISAACRandom(int[] seed) Creates a new ISAAC random number generator using an int array seed.- Parameters:
seed
- Initial seed. Ifnull
, the seed will be related to the current time.
-
-
Method Details
-
setSeed
public void setSeed(int seed) Sets the seed of the underlying random number generator using anint
seed.Sequences of values generated starting with the same seeds should be identical.
- Specified by:
setSeed
in interfaceRandomGenerator
- Specified by:
setSeed
in classBitsStreamGenerator
- Parameters:
seed
- the seed value
-
setSeed
public void setSeed(long seed) Sets the seed of the underlying random number generator using along
seed.Sequences of values generated starting with the same seeds should be identical.
- Specified by:
setSeed
in interfaceRandomGenerator
- Specified by:
setSeed
in classBitsStreamGenerator
- Parameters:
seed
- the seed value
-
setSeed
public void setSeed(int[] seed) Sets the seed of the underlying random number generator using anint
array seed.Sequences of values generated starting with the same seeds should be identical.
- Specified by:
setSeed
in interfaceRandomGenerator
- Specified by:
setSeed
in classBitsStreamGenerator
- Parameters:
seed
- the seed value
-
next
protected int next(int bits) Generate next pseudorandom number.This method is the core generation algorithm. It is used by all the public generation methods for the various primitive types
BitsStreamGenerator.nextBoolean()
,BitsStreamGenerator.nextBytes(byte[])
,BitsStreamGenerator.nextDouble()
,BitsStreamGenerator.nextFloat()
,BitsStreamGenerator.nextGaussian()
,BitsStreamGenerator.nextInt()
,BitsStreamGenerator.next(int)
andBitsStreamGenerator.nextLong()
.- Specified by:
next
in classBitsStreamGenerator
- Parameters:
bits
- number of random bits to produce- Returns:
- random bits generated
-
isaac
private void isaac()Generate 256 results -
isaac2
private void isaac2()Intermediate internal loop. -
isaac3
private void isaac3()Lowest level internal loop. -
initState
private void initState()Initialize, or reinitialize, this instance of rand. -
shuffle
private void shuffle()Shuffle array. -
setState
private void setState(int start) Set the state by copying the internal arrays.- Parameters:
start
- First index intomem
array.
-