Interface Pcg
-
- All Known Implementing Classes:
PcgRR,PcgRRCas,PcgRRLocked,PcgRS,PcgRSCas,PcgRSFast,PcgRSLocked,RandomBase64,RandomBaseCAS,RandomBaseLocked,RandomBaseSynchonized
public interface PcgBase interface for all PCG random generators. The PCG family uses a linear congruential generator as the state-transition function—the “CG” of PCG stands for “congruential generator”. Linear congruential generators are known to be statistically weak.PCG uses a new technique called permutation functions on tuples to produce output that is much more random than the RNG's internal state. The output function is defined by the extending classes.
A paper highlighting the individual properties can be found here. http://www.pcg-random.org/paper.html. This class is an adaption to the original c source code provided by M.E. O'Neill.
- Author:
- Kilian
- See Also:
- www.pcg-random.org
-
-
Field Summary
Fields Modifier and Type Field Description static java.util.concurrent.atomic.AtomicLongUNIQUE_SEEDEnsure that a unique seed is used for randomly seeded instances
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description voidadvance(long steps)Advance or set back the rngs state.default longdistance(Pcg other)Return the distance between the two generators.default longdistanceUnsafe(Pcg other)Return the distance between the two generators.longgetInc()Returns the internal increment of the congurential generator used by this pcglonggetMult()Returns the internal multiplication of the congurential generator used by this pcglonggetState()Returns the internal state of the congruential generator used by this pcgbooleanisFast()Return true if this rng is a fast instance.intnext(int n)Returns an integer with the next n low bits randomly set and are used as a base to deviate smaller data types.booleannextBoolean()Returns the next pseudorandom, uniformly distributedbooleanvalue from this random number generator's sequence.booleannextBoolean(double probability)Returns the next pseudorandombooleanvalue from this random number generator's sequence with the given probability of being true.bytenextByte()Returns the next pseudorandom, uniformly distributedbytevalue from this random number generator's sequence.voidnextBytes(byte[] bytes)Generates random bytes and places them into a user-supplied byte array.charnextChar()Returns the next pseudorandom, uniformly distributedcharvalue from this random number generator's sequence.doublenextDouble()Returns the next pseudorandom, uniformly distributeddoublevalue between0.0and1.0from this random number generator's sequence.doublenextDouble(boolean includeZero, boolean includeOne)Returns the next pseudorandom, uniformly distributeddoublevalue in the range from 0.0 to 1.0, possibly inclusive of 0.0 and 1.0 themselves.floatnextFloat()Returns the next pseudorandom, uniformly distributedfloatvalue between0.0and1.0from this random number generator's sequence.floatnextFloat(boolean includeZero, boolean includeOne)Returns the next pseudorandom, uniformly distributedfloatvaluet in the range from 0.0f to 1.0f, possibly inclusive of 0.0f and 1.0f themselves.doublenextGaussian()intnextInt()Returns the next pseudorandom, uniformly distributedintvalue from this random number generator's sequence.intnextInt(int n)Returns a pseudorandom, uniformly distributedintvalue between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.longnextLong()Returns the next pseudorandom, uniformly distributedlongvalue from this random number generator's sequence.longnextLong(long n)Returns the next pseudorandom, uniformly distributedlongvalue from this random number generator's sequence.shortnextShort()Returns the next pseudorandom, uniformly distributedshortvalue from this random number generator's sequence.<T> Tsplit()Splits the generator in a copy with the exact same state and stream number.<T> TsplitDistinct()Splits the generator in a copy with distinct state and stream number.
-
-
-
Method Detail
-
advance
void advance(long steps)
Advance or set back the rngs state. In other words fast skip the next n generated random numbers or set the PNG back so it will create the last n numbers in the same sequence again.int x = nextInt(); nextInt(); nextInt(); step(-3); int y = nextInt(); x == y TRUEBe aware that this relationship is only true for deterministic generation calls.nextGaussian()or any bound limited number generations might loop and consume more than one step to generate a number.To advance n steps the function performs
Math.ceil( log2(n) )iterations. So you may go ahead and skip as many steps as you like without any performance implications.Negative indices can be used to jump backwards in time going the long way around
- Parameters:
steps- the amount of steps to advance or in case of a negative number go back in history
-
split
<T> T split() throws java.lang.ReflectiveOperationExceptionSplits the generator in a copy with the exact same state and stream number. The produced generators don't share any state variables enabling to generate random numbers without impacting the other generator. While the states are independent they are exact copies resulting in the generated numbers to follow the same sequence.On the other hand
splitDistinct()will return a generator who has a different state and stream number ensuring that the generated sequence is NOT the same as this generator.- Type Parameters:
T- Class of the constructed generator which is equals the class this method was invoked on.- Returns:
- an identical generator with no shared references
- Throws:
java.lang.ReflectiveOperationException- if the extending class does not implement the required constructor
-
splitDistinct
<T> T splitDistinct() throws java.lang.ReflectiveOperationExceptionSplits the generator in a copy with distinct state and stream number. The produced generators don't share any state variables enabling to generate random numbers without impacting the other generator. The generators are guaranteed to produce different sequences of numbers and can't be used independently of each other.On the other hand
split()will return a generator who has an identical state and stream number ensuring that the generated sequence is the same as this generator.- Type Parameters:
T- Class of the constructed generator which is equals the class this method was invoked on.- Returns:
- a distinct generator with no shared references
- Throws:
java.lang.ReflectiveOperationException- if the extending class does not implement the required constructor
-
next
int next(int n)
Returns an integer with the next n low bits randomly set and are used as a base to deviate smaller data types. The used bits are the high bits used from the underlying integer. An n of more 31 bits will result in no bits being set, thus returning 0.- Parameters:
n- the number of randomly set bits. Must be positive and does not produce reasonable results for > 31- Returns:
- an integer
-
nextBoolean
boolean nextBoolean()
Returns the next pseudorandom, uniformly distributedbooleanvalue from this random number generator's sequence. The general contract ofnextBooleanis that onebooleanvalue is pseudorandomly generated and returned. All possiblebooleanvalues are produced with (approximately) equal probability.- Returns:
- the next pseudorandom, uniformly distributed
booleanvalue from this random number generator's sequence
-
nextBoolean
boolean nextBoolean(double probability)
Returns the next pseudorandombooleanvalue from this random number generator's sequence with the given probability of being true.A probability value of 0 will always return
falseand a value of 1 will always returntrue.- Parameters:
probability- the probability of the returned boolean to be true in range of [0-1]- Returns:
- the next pseudorandom boolean with given probability to tbe true
- Throws:
java.lang.IllegalArgumentException- if probability is > 1 or < 0
-
nextBytes
void nextBytes(byte[] bytes)
Generates random bytes and places them into a user-supplied byte array. The number of random bytes produced is equal to the length of the byte array.- Parameters:
bytes- the byte array to fill with random bytes- Throws:
java.lang.NullPointerException- if the byte array is null- See Also:
nextByte()
-
nextChar
char nextChar()
Returns the next pseudorandom, uniformly distributedcharvalue from this random number generator's sequence. The general contract ofnextCharis that onecharvalue is pseudorandomly generated and returned. All possiblecharvalues are produced with (approximately) equal probability.- Returns:
- the next pseudorandom, uniformly distributed
charvalue from this random number generator's sequence
-
nextShort
short nextShort()
Returns the next pseudorandom, uniformly distributedshortvalue from this random number generator's sequence. The general contract ofnextShortis that oneshortvalue is pseudorandomly generated and returned. All possibleshortvalues are produced with (approximately) equal probability.- Returns:
- the next pseudorandom, uniformly distributed
shortvalue from this random number generator's sequence
-
nextByte
byte nextByte()
Returns the next pseudorandom, uniformly distributedbytevalue from this random number generator's sequence. The general contract ofnextByteis that onebytevalue is pseudorandomly generated and returned. All possiblebytevalues are produced with (approximately) equal probability.- Returns:
- the next pseudorandom, uniformly distributed
bytevalue from this random number generator's sequence
-
nextInt
int nextInt()
Returns the next pseudorandom, uniformly distributedintvalue from this random number generator's sequence. The general contract ofnextIntis that oneintvalue is pseudorandomly generated and returned. All 232 possibleintvalues are produced with (approximately) equal probability.- Returns:
- the next pseudorandom, uniformly distributed
intvalue from this random number generator's sequence
-
nextInt
int nextInt(int n)
Returns a pseudorandom, uniformly distributedintvalue between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence. The general contract ofnextIntis that oneintvalue in the specified range is pseudorandomly generated and returned. Allboundpossibleintvalues are produced with (approximately) equal probability.- Parameters:
n- the upper bound (exclusive). Must be positive.- Returns:
- the next pseudorandom, uniformly distributed
intvalue between zero (inclusive) andbound(exclusive) from this random number generator's sequence - Throws:
java.lang.IllegalArgumentException- if bound is not positive- See Also:
nextInt()
-
nextLong
long nextLong()
Returns the next pseudorandom, uniformly distributedlongvalue from this random number generator's sequence. The general contract ofnextLongis that onelongvalue is pseudorandomly generated and returned.- Returns:
- the next pseudorandom, uniformly distributed
longvalue from this random number generator's sequence
-
nextLong
long nextLong(long n)
Returns the next pseudorandom, uniformly distributedlongvalue from this random number generator's sequence. The general contract ofnextLongis that onelongvalue is pseudorandomly generated and returned.- Parameters:
n- the upper bound (exclusive). Must be positive.- Returns:
- the next pseudorandom, uniformly distributed
longvalue from this random number generator's sequence
-
nextFloat
float nextFloat()
Returns the next pseudorandom, uniformly distributedfloatvalue between0.0and1.0from this random number generator's sequence.The general contract of
nextFloatis that onefloatvalue, chosen (approximately) uniformly from the range0.0f(inclusive) to1.0f(exclusive), is pseudorandomly generated and returned. All 224 possiblefloatvalues of the form m x 2-24, where m is a positive integer less than 224, are produced with (approximately) equal probability.- Returns:
- the next pseudorandom, uniformly distributed
floatvalue between0.0and1.0from this random number generator's sequence
-
nextFloat
float nextFloat(boolean includeZero, boolean includeOne)Returns the next pseudorandom, uniformly distributedfloatvaluet in the range from 0.0f to 1.0f, possibly inclusive of 0.0f and 1.0f themselves. Thus:Expression Interval nextFloat(false, false) (0.0f, 1.0f) nextFloat(true, false) [0.0f, 1.0f) nextFloat(false, true) (0.0f, 1.0f] nextFloat(true, true) [0.0f, 1.0f] Table of intervals This version preserves all possible random values in the float range.
- Parameters:
includeZero- if true may return 0fincludeOne- if true may return 1f- Returns:
- the next pseudorandom, uniformly distributed
floatvalue from this random number generator's sequence - See Also:
nextFloat()
-
nextDouble
double nextDouble()
Returns the next pseudorandom, uniformly distributeddoublevalue between0.0and1.0from this random number generator's sequence.The general contract of
nextDoubleis that onedoublevalue, chosen (approximately) uniformly from the range0.0d(inclusive) to1.0d(exclusive), is pseudorandomly generated and returned.- Returns:
- the next pseudorandom, uniformly distributed
doublevalue between0.0and1.0from this random number generator's sequence
-
nextDouble
double nextDouble(boolean includeZero, boolean includeOne)Returns the next pseudorandom, uniformly distributeddoublevalue in the range from 0.0 to 1.0, possibly inclusive of 0.0 and 1.0 themselves. Thus:Expression Interval nextDouble(false, false) (0.0, 1.0) nextDouble(true, false) [0.0, 1.0) nextDouble(false, true) (0.0, 1.0] nextDouble(true, true) [0.0, 1.0] Table of intervals This version preserves all possible random values in the double range.
- Parameters:
includeZero- if true may return 0dincludeOne- if true may return 1d- Returns:
- the next pseudorandom, uniformly distributed
doublevalue from this random number generator's sequence - See Also:
nextDouble()
-
nextGaussian
double nextGaussian()
-
distance
default long distance(Pcg other)
Return the distance between the two generators. The distance is the number of steps this generator is ahead or behind the other generator. After advancing or rewinding the amount of steps returned by this function both instances have caught up and will return the same value for primitive nextX() function calls.Be aware that this guarantee only holds true for single threaded instances. An exception will be thrown if the comparing objects are not from the same class. If you regardlessly want to compare two generators for which
getClass() != other.getClass()is true but may still reach the same state (e.g. a non fast and fast or locked and cas) implementation have a look atdistanceUnsafe(Pcg)which does not check for class safety.- Parameters:
other- the generator to compare this instance to- Returns:
- the distance between the two generators
- Throws:
IncompatibleGeneratorException- if the other generator isn't of the same class as this generator or the increment of the generators is distinct resulting the generators to never be able to reach the same state.- See Also:
distanceUnsafe(Pcg)
-
distanceUnsafe
default long distanceUnsafe(Pcg other)
Return the distance between the two generators. The distance is the number of steps this generator is ahead or behind the other generator. After advancing or rewinding the amount of steps returned by this function both instances have caught up and will return the same value for primitive nextX() function calls.Be aware that this guarantee only holds true for single threaded instances. This method does Not check if the 2 instances of the generators are of the same class, employ the same algorithm. This may result in undetermined results being returned if the algorithm terminates at all. For a save version take a look at
distance(Pcg)- Parameters:
other- the generator to compare this instance to- Returns:
- the distance between the two generators
- Throws:
IncompatibleGeneratorException- if the increment of the generators is distinct resulting the generators to never be able to reach the same state.- See Also:
distance(Pcg)
-
isFast
boolean isFast()
Return true if this rng is a fast instance. This check is mostly used int he distance calculation due to the fact that the state of fast RNGs is shifted by one. They first calculate a new value and directly use it instead of using the old state and calculating a new one- Returns:
- true if the subclass uses the newly generated state directly
-
getState
long getState()
Returns the internal state of the congruential generator used by this pcg- Returns:
- the internal state
-
getInc
long getInc()
Returns the internal increment of the congurential generator used by this pcg- Returns:
- the increment
-
getMult
long getMult()
Returns the internal multiplication of the congurential generator used by this pcg- Returns:
- the multiplication factor
-
-