public class MiddleSquareWeylSequence extends IntProvider
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.
Constructor and Description |
---|
MiddleSquareWeylSequence(long[] seed)
Creates a new instance.
|
Modifier and Type | Method and Description |
---|---|
protected byte[] |
getStateInternal()
Creates a snapshot of the RNG state.
|
int |
next() |
long |
nextLong() |
protected void |
setStateInternal(byte[] state)
Resets the RNG to the given
state . |
nextBoolean, nextBytes, nextBytes, nextDouble, nextInt, resetCachedState
checkIndex, checkStateSize, composeStateInternal, extendSeed, extendSeed, fillState, fillState, restoreState, saveState, splitStateInternal, toString
public MiddleSquareWeylSequence(long[] seed)
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 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.
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.protected byte[] getStateInternal()
getStateInternal
in class IntProvider
protected void setStateInternal(byte[] state)
state
.setStateInternal
in class IntProvider
state
- State (previously obtained by a call to
BaseProvider.getStateInternal()
).BaseProvider.checkStateSize(byte[],int)
public int next()
public long nextLong()
nextLong
in interface UniformRandomProvider
nextLong
in class IntProvider
Copyright © 2016–2022 The Apache Software Foundation. All rights reserved.