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 intCount through the results in rsl[].private static final intThe golden ratio.private static final intHalf-size of rsl[] and mem[].private intAccumulator.private intThe last result.private intCounter, guarantees cycle is at least 2^40.private intService variable.private intService variable.private intService variable.private static final intFor pseudo-random lookup.private final int[]The internal state.private final int[]The results given to the user.private static final intSize of rsl[] and mem[].private static final intLog of size of rsl[] and mem[]. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected byte[]Creates a snapshot of the RNG state.private voidInitialize, 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, resetCachedStateMethods 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:
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:
-
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.
-