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[]arrService variable.private intcountCount through the results in rsl[].private static intGLD_RATIOThe golden ratio.private static intH_SIZEHalf-size of rsl[] and mem[].private intisaacAAccumulator.private intisaacBThe last result.private intisaacCCounter, guarantees cycle is at least 2^40.private intisaacIService variable.private intisaacJService variable.private intisaacXService variable.private static intMASKFor pseudo-random lookup.private int[]memThe internal state.private int[]rslThe results given to the user.private static intSIZESize of rsl[] and mem[].private static intSIZE_LLog 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 voidinitState()Initialize, or reinitialize, this instance of rand.private voidisaac()Generate 256 results.private voidisaac2()Intermediate internal loop.private voidisaac3()Lowest level internal loop.intnext()Return the next random value.private voidsetSeedInternal(int[] seed)Reseeds the RNG.private voidsetState(int start)Set the state by copying the internal arrays.protected voidsetStateInternal(byte[] s)Resets the RNG to the givenstate.private voidshuffle()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:
getStateInternalin classIntProvider- Returns:
- the internal state.
-
setStateInternal
protected void setStateInternal(byte[] s)
Resets the RNG to the givenstate.- Overrides:
setStateInternalin 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 intomemarray.
-
-