Class AbstractPcg6432
- java.lang.Object
-
- org.apache.commons.rng.core.BaseProvider
-
- org.apache.commons.rng.core.source32.IntProvider
-
- org.apache.commons.rng.core.source32.AbstractPcg6432
-
- All Implemented Interfaces:
RandomIntSource
,RestorableUniformRandomProvider
,UniformRandomProvider
- Direct Known Subclasses:
PcgXshRr32
,PcgXshRs32
abstract class AbstractPcg6432 extends IntProvider
This abstract class is a base for algorithms from the Permuted Congruential Generator (PCG) family that use an internal 64-bit Linear Congruential Generator (LCG) and output 32-bits per cycle.Note: PCG generators may exhibit massive stream correlation
Although the seed size is 128 bits, only the first 64 are effective: in effect, two seeds that only differ by the last 64 bits may produce highly correlated sequences.
Due to the use of an underlying linear congruential generator (LCG) alterations to the 128 bit seed have the following effect: the first 64-bits alter the generator state; the second 64 bits, with the exception of the most significant bit, which is discarded, choose between one of two alternative LCGs where the output of the chosen LCG is the same sequence except for an additive constant determined by the seed bits. The result is that seeds that differ only in the last 64-bits will have a 50% chance of producing highly correlated output sequences.
Consider using the fixed increment variant where the 64-bit seed sets the generator state.
For further information see:
-
Durst, M.J. (1989) Using Linear Congruential Generators For Parallel Random Number Generation. Section 3.1: Different additive constants in a maximum potency congruential generator. 1989 Winter Simulation Conference Proceedings, Washington, DC, USA, 1989, pp. 462-466.
-
-
Field Summary
Fields Modifier and Type Field Description private static long
DEFAULT_INCREMENT
The default increment.private long
increment
The increment of the LCG.private static int
SEED_SIZE
Size of the seed array.private long
state
The state of the LCG.
-
Constructor Summary
Constructors Constructor Description AbstractPcg6432(long[] seed)
Creates a new instance.AbstractPcg6432(java.lang.Long seed)
Creates a new instance using a default increment.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description private long
bump(long input)
Provides the next state of the LCG.protected byte[]
getStateInternal()
Creates a snapshot of the RNG state.int
next()
private void
setSeedInternal(long[] seed)
Seeds the RNG.protected void
setStateInternal(byte[] s)
Resets the RNG to the givenstate
.protected abstract int
transform(long x)
Transform the 64-bit state of the generator to a 32-bit output.-
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 Detail
-
SEED_SIZE
private static final int SEED_SIZE
Size of the seed array.- See Also:
- Constant Field Values
-
DEFAULT_INCREMENT
private static final long DEFAULT_INCREMENT
The default increment.- See Also:
- Constant Field Values
-
state
private long state
The state of the LCG.
-
increment
private long increment
The increment of the LCG.
-
-
Constructor Detail
-
AbstractPcg6432
AbstractPcg6432(java.lang.Long seed)
Creates a new instance using a default increment.- Parameters:
seed
- Initial state.- Since:
- 1.4
-
AbstractPcg6432
AbstractPcg6432(long[] seed)
Creates a new instance.- Parameters:
seed
- Initial seed. If the length is larger than 2, only the first 2 elements will be used; if smaller, the remaining elements will be automatically set.The 1st element is used to set the LCG state. The 2nd element is used to set the LCG increment; the most significant bit is discarded by left shift and the increment is set to odd.
-
-
Method Detail
-
setSeedInternal
private void setSeedInternal(long[] seed)
Seeds the RNG.- Parameters:
seed
- Seed.
-
bump
private long bump(long input)
Provides the next state of the LCG.- Parameters:
input
- Current state.- Returns:
- next state
-
next
public int next()
- Returns:
- the next random value.
-
transform
protected abstract int transform(long x)
Transform the 64-bit state of the generator to a 32-bit output. The transformation function shall vary with respect to different generators.- Parameters:
x
- State.- Returns:
- the output
-
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:
BaseProvider.checkStateSize(byte[],int)
-
-