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
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:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final int[]
Service variable.private int
Count through the results in rsl[].private static final int
The golden ratio.private static final int
Half-size of rsl[] and mem[].private int
Accumulator.private int
The last result.private int
Counter, guarantees cycle is at least 2^40.private int
Service variable.private int
Service variable.private int
Service variable.private static final int
For pseudo-random lookup.private final int[]
The internal state.private final int[]
The results given to the user.private static final int
Size of rsl[] and mem[].private static final int
Log of size of rsl[] and mem[]. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected byte[]
Creates a snapshot of the RNG state.private void
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()
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 Details
-
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(int[] seed) Creates a new ISAAC random number generator.- Parameters:
seed
- Initial seed
-
-
Method Details
-
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:
-
setSeedInternal
private void setSeedInternal(int[] seed) Reseeds the RNG.- Parameters:
seed
- Seed. Cannot be null.
-
next
public int next()- 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.
-