Class MultiplyWithCarry256
java.lang.Object
org.apache.commons.rng.core.BaseProvider
org.apache.commons.rng.core.source32.IntProvider
org.apache.commons.rng.core.source32.MultiplyWithCarry256
- All Implemented Interfaces:
RandomIntSource, RestorableUniformRandomProvider, UniformRandomProvider
Port from Marsaglia's
"Multiply-With-Carry" algorithm.
Implementation is based on the (non-portable!) C code reproduced on that page.
- Since:
- 1.0
- See Also:
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected byte[]Creates a snapshot of the RNG state.intnext()Return the next random value.private voidsetSeedInternal(int[] seed) Seeds the RNG.protected voidsetStateInternal(byte[] s) Resets the RNG to the givenstate.Methods inherited from class IntProvider
nextBoolean, nextBytes, nextBytes, nextBytesFill, nextDouble, nextInt, nextLong, resetCachedStateMethods inherited from class BaseProvider
checkIndex, checkStateSize, composeStateInternal, extendSeed, extendSeed, fillState, fillState, restoreState, saveState, splitStateInternal, toString
-
Field Details
-
Q_SIZE
private static final int Q_SIZELength of the state array.- See Also:
-
SEED_SIZE
private static final int SEED_SIZESize of the seed.- See Also:
-
A
private static final long AMultiply.- See Also:
-
state
private final int[] stateState. -
index
private int indexCurrent index in "state" array. -
carry
private int carryCarry.
-
-
Constructor Details
-
MultiplyWithCarry256
public MultiplyWithCarry256(int[] seed) Creates a new instance.- Parameters:
seed- Seed. If the length is larger than 257, only the first 257 elements will be used; if smaller, the remaining elements will be automatically set.
-
-
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) Seeds the RNG.- Parameters:
seed- Seed.
-
next
public int next()Return the next random value.- Returns:
- the next random value.
-