Class ISAACRandom
- java.lang.Object
-
- org.apache.commons.rng.core.BaseProvider
-
- org.apache.commons.rng.core.source32.IntProvider
-
- org.apache.commons.rng.core.source32.ISAACRandom
-
- All Implemented Interfaces:
RandomIntSource
,RestorableUniformRandomProvider
,UniformRandomProvider
public class ISAACRandom extends IntProvider
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.
- Since:
- 1.0
- See Also:
- ISAAC: a fast cryptographic pseudo-random number generator, ISAAC (Wikipedia)
-
-
Field Summary
Fields Modifier and Type Field Description private int[]
arr
Service variable.private int
count
Count through the results in rsl[].private static int
GLD_RATIO
The golden ratio.private static int
H_SIZE
Half-size of rsl[] and mem[].private int
isaacA
Accumulator.private int
isaacB
The last result.private int
isaacC
Counter, guarantees cycle is at least 2^40.private int
isaacI
Service variable.private int
isaacJ
Service variable.private int
isaacX
Service variable.private static int
MASK
For pseudo-random lookup.private int[]
mem
The internal state.private int[]
rsl
The results given to the user.private static int
SIZE
Size of rsl[] and mem[].private static int
SIZE_L
Log of size of rsl[] and mem[].
-
Constructor Summary
Constructors Constructor Description ISAACRandom(int[] seed)
Creates a new ISAAC random number generator.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected byte[]
getStateInternal()
Creates a snapshot of the RNG state.private void
initState()
Initialize, or reinitialize, this instance of rand.private void
isaac()
Generate 256 results.private void
isaac2()
Intermediate internal loop.private void
isaac3()
Lowest level internal loop.int
next()
Return the next random value.private void
setSeedInternal(int[] seed)
Reseeds the RNG.private void
setState(int start)
Set the state by copying the internal arrays.protected void
setStateInternal(byte[] s)
Resets the RNG to the givenstate
.private void
shuffle()
Shuffle array.-
Methods inherited from class org.apache.commons.rng.core.source32.IntProvider
nextBoolean, nextBytes, nextBytes, nextBytesFill, nextDouble, nextInt, nextLong, resetCachedState
-
Methods inherited from class org.apache.commons.rng.core.BaseProvider
checkIndex, checkStateSize, composeStateInternal, extendSeed, extendSeed, fillState, fillState, restoreState, saveState, splitStateInternal, toString
-
-
-
-
Field Detail
-
SIZE_L
private static final int SIZE_L
Log of size of rsl[] and mem[].- See Also:
- Constant Field Values
-
SIZE
private static final int SIZE
Size of rsl[] and mem[].- See Also:
- Constant Field Values
-
H_SIZE
private static final int H_SIZE
Half-size of rsl[] and mem[].- See Also:
- Constant Field Values
-
MASK
private static final int MASK
For pseudo-random lookup.- See Also:
- Constant Field Values
-
GLD_RATIO
private static final int GLD_RATIO
The golden ratio.- See Also:
- Constant Field Values
-
rsl
private final int[] rsl
The results given to the user.
-
mem
private final int[] mem
The internal state.
-
count
private int count
Count through the results in rsl[].
-
isaacA
private int isaacA
Accumulator.
-
isaacB
private int isaacB
The last result.
-
isaacC
private int isaacC
Counter, guarantees cycle is at least 2^40.
-
arr
private final int[] arr
Service variable.
-
isaacX
private int isaacX
Service variable.
-
isaacI
private int isaacI
Service variable.
-
isaacJ
private int isaacJ
Service variable.
-
-
Method Detail
-
getStateInternal
protected byte[] getStateInternal()
Creates a snapshot of the RNG state.- Overrides:
getStateInternal
in classIntProvider
- Returns:
- the internal state.
-
setStateInternal
protected void setStateInternal(byte[] s)
Resets the RNG to the givenstate
.- Overrides:
setStateInternal
in classIntProvider
- Parameters:
s
- State (previously obtained by a call toBaseProvider.getStateInternal()
).- See Also:
BaseProvider.checkStateSize(byte[],int)
-
setSeedInternal
private void setSeedInternal(int[] seed)
Reseeds the RNG.- Parameters:
seed
- Seed. Cannot be null.
-
next
public int next()
Return the next random value.- Returns:
- the next random value.
-
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.
-
-