Class MiddleSquareWeylSequence
- java.lang.Object
-
- org.apache.commons.rng.core.BaseProvider
-
- org.apache.commons.rng.core.source32.IntProvider
-
- org.apache.commons.rng.core.source32.MiddleSquareWeylSequence
-
- All Implemented Interfaces:
RandomIntSource
,RestorableUniformRandomProvider
,UniformRandomProvider
public class MiddleSquareWeylSequence extends IntProvider
Middle Square Weyl Sequence Random Number Generator.A fast all-purpose 32-bit generator. Memory footprint is 192 bits and the period is at least
2^64
.Implementation is based on the paper Middle Square Weyl Sequence RNG.
- Since:
- 1.3
- See Also:
- Middle Square Method
-
-
Field Summary
Fields Modifier and Type Field Description private static long[]
DEFAULT_SEED
The default seed.private long
s
Increment for the Weyl sequence.private static int
SEED_SIZE
Size of the seed array.private long
w
State of the Weyl sequence.private long
x
State of the generator.
-
Constructor Summary
Constructors Constructor Description MiddleSquareWeylSequence(long[] seed)
Creates a new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected byte[]
getStateInternal()
Creates a snapshot of the RNG state.int
next()
Return the next random value.long
nextLong()
Generates along
value.private void
setSeedInternal(long[] seed)
Seeds the RNG.protected void
setStateInternal(byte[] state)
Resets the RNG to the givenstate
.-
Methods inherited from class org.apache.commons.rng.core.source32.IntProvider
nextBoolean, nextBytes, nextBytes, nextBytesFill, nextDouble, nextInt, resetCachedState
-
Methods inherited from class org.apache.commons.rng.core.BaseProvider
checkIndex, checkStateSize, composeStateInternal, extendSeed, extendSeed, fillState, fillState, restoreState, saveState, splitStateInternal, toString
-
-
-
-
Field Detail
-
SEED_SIZE
private static final int SEED_SIZE
Size of the seed array.- See Also:
- Constant Field Values
-
DEFAULT_SEED
private static final long[] DEFAULT_SEED
The default seed. This has a high quality Weyl increment (containing many bit state transitions).
-
x
private long x
State of the generator.
-
w
private long w
State of the Weyl sequence.
-
s
private long s
Increment for the Weyl sequence. This must be odd to ensure a full period.This is not final to support the restore functionality.
-
-
Constructor Detail
-
MiddleSquareWeylSequence
public MiddleSquareWeylSequence(long[] seed)
Creates a new instance.Note: The generator output quality is highly dependent on the initial seed. If the generator is seeded poorly (for example with all zeros) it is possible the generator may output zero for many cycles before the internal state recovers randomness. The seed elements are used to set:
- The state of the generator
- The state of the Weyl sequence
- The increment of the Weyl sequence
The third element is set to odd to ensure a period of at least 264. If the increment is of low complexity then the Weyl sequence does not contribute high quality randomness. It is recommended to use a permutation of 8 hex characters for the upper and lower 32-bits of the increment.
The state of the generator is squared during each cycle. There is a possibility that different seeds can produce the same output, for example 0 and 232 produce the same square. This can be avoided by using the high complexity Weyl increment for the state seed element.
- Parameters:
seed
- Initial seed. If the length is larger than 3, only the first 3 elements will be used; if smaller, the remaining elements will be automatically set.
-
-
Method Detail
-
setSeedInternal
private void setSeedInternal(long[] seed)
Seeds the RNG.- Parameters:
seed
- Seed.
-
getStateInternal
protected byte[] getStateInternal()
Creates a snapshot of the RNG state.- Overrides:
getStateInternal
in classIntProvider
- Returns:
- the internal state.
-
setStateInternal
protected void setStateInternal(byte[] state)
Resets the RNG to the givenstate
.- Overrides:
setStateInternal
in classIntProvider
- Parameters:
state
- State (previously obtained by a call toBaseProvider.getStateInternal()
).- See Also:
BaseProvider.checkStateSize(byte[],int)
-
next
public int next()
Return the next random value.- Returns:
- the next random value.
-
nextLong
public long nextLong()
Generates along
value.- Specified by:
nextLong
in interfaceUniformRandomProvider
- Overrides:
nextLong
in classIntProvider
- Returns:
- the next random value.
-
-