Class PcgRSFast
- java.lang.Object
-
- java.util.Random
-
- com.github.kilianB.pcg.fast.PcgRSFast
-
- All Implemented Interfaces:
Pcg,java.io.Serializable
public class PcgRSFast extends java.util.Random implements Pcg
A 64 bit State PcgRNG with 32 bit output. PCG-XSH-RRThe pcg family combines a linear congruential generators with a permutation output function resulting in high quality pseudo random numbers.
The original concept was introduced by Melissa O’Neill please refer to pcg-random for more information.
Opposed to RR this version performs a random shift rather than a random rotation. The RS instance permutates the output using the following function:
This implementation is Not thread safe, inlines most methods manually and performs other optimizations to maximize the throughput. While the basic methods to retrieve datatypes are implemented no guarantee is made regarding the stream and splitterator methods provided by the Random class. Additional tests have to be performed if they impact the internal state of this class in a harmful way.((state >>> 22) ^ state) >>> ((state >>> 61) + 22)- Author:
- Kilian
- See Also:
- www.pcg-random.org, Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected longincStream number of the rng.protected longstate64 bit internal state-
Fields inherited from interface com.github.kilianB.pcg.Pcg
UNIQUE_SEED
-
-
Constructor Summary
Constructors Modifier Constructor Description PcgRSFast()Create a PcgRSFast instance seeded with with 2 longs generated by xorshift*.PcgRSFast(long seed, long streamNumber)Create a random number generator with the given seed and stream number.protectedPcgRSFast(long initialState, long increment, boolean dummy)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadvance(long steps)Advance or set back the rngs state.longgetInc()Returns the internal increment of the congurential generator used by this pcglonggetMult()Returns the internal multiplication of the congurential generator used by this pcgprotected static longgetRandomSeed()longgetState()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[] b)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.protected voidsetInc(long increment)voidsetSeed(long seed, long streamNumber)Sets the seed of this random number generator using .protected voidsetState(long state)<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.-
Methods inherited from class java.util.Random
doubles, doubles, doubles, doubles, ints, ints, ints, ints, longs, longs, longs, longs, setSeed
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.github.kilianB.pcg.Pcg
distance, distanceUnsafe
-
-
-
-
Constructor Detail
-
PcgRSFast
public PcgRSFast()
Create a PcgRSFast instance seeded with with 2 longs generated by xorshift*. The values chosen are very likely not used as seeds in any other non argument constructor of any of the classes provided in this library.
-
PcgRSFast
public PcgRSFast(long seed, long streamNumber)Create a random number generator with the given seed and stream number. The seed defines the current state in which the rng is in and corresponds to seeds usually found in other RNG instances. RNGs with different seeds are able to catch up after they exhaust their period and produce the same numbers. (2^63).Different stream numbers alter the increment of the rng and ensure distinct state sequences
Only generators with the same seed AND stream numbers will produce identical values
- Parameters:
seed- used to compute the starting state of the RNGstreamNumber- used to compute the increment for the lcg.
-
PcgRSFast
protected PcgRSFast(long initialState, long increment, boolean dummy)
-
-
Method Detail
-
setSeed
public void setSeed(long seed, long streamNumber)Sets the seed of this random number generator using . The general contract of setSeed is that it alters the state of this random number generator object so as to be in exactly the same state as if it had just been created with the argument seed as a seed. Only generators with the same seed AND stream numbers will produce identical values- Parameters:
seed- used to compute the starting state of the RNGstreamNumber- used to compute the increment for the lcg.
-
advance
public 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
-
nextByte
public byte nextByte()
Description copied from interface:PcgReturns 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.
-
nextBytes
public void nextBytes(byte[] b)
Description copied from interface:PcgGenerates 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.- Specified by:
nextBytesin interfacePcg- Overrides:
nextBytesin classjava.util.Random- Parameters:
b- the byte array to fill with random bytes- See Also:
Pcg.nextByte()
-
nextChar
public char nextChar()
Description copied from interface:PcgReturns 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.
-
nextShort
public short nextShort()
Description copied from interface:PcgReturns 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.
-
nextInt
public 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.
-
nextInt
public 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.- Specified by:
nextIntin interfacePcg- Overrides:
nextIntin classjava.util.Random- 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 - See Also:
Pcg.nextInt()
-
nextBoolean
public 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. The valuestrueandfalseare produced with (approximately) equal probability.- Specified by:
nextBooleanin interfacePcg- Overrides:
nextBooleanin classjava.util.Random- Returns:
- the next pseudorandom, uniformly distributed
booleanvalue from this random number generator's sequence
-
nextBoolean
public boolean nextBoolean(double probability)
Description copied from interface:PcgReturns 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.- Specified by:
nextBooleanin interfacePcg- 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
-
nextLong
public long nextLong()
Description copied from interface:PcgReturns the next pseudorandom, uniformly distributedlongvalue from this random number generator's sequence. The general contract ofnextLongis that onelongvalue is pseudorandomly generated and returned.
-
nextLong
public long nextLong(long n)
Description copied from interface:PcgReturns the next pseudorandom, uniformly distributedlongvalue from this random number generator's sequence. The general contract ofnextLongis that onelongvalue is pseudorandomly generated and returned.
-
nextDouble
public double nextDouble()
Description copied from interface:PcgReturns 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.- Specified by:
nextDoublein interfacePcg- Overrides:
nextDoublein classjava.util.Random- Returns:
- the next pseudorandom, uniformly distributed
doublevalue between0.0and1.0from this random number generator's sequence
-
nextDouble
public double nextDouble(boolean includeZero, boolean includeOne)Description copied from interface:PcgReturns 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.
- Specified by:
nextDoublein interfacePcg- 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:
Pcg.nextDouble()
-
nextFloat
public float nextFloat()
Description copied from interface:PcgReturns 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.
-
nextFloat
public float nextFloat(boolean includeZero, boolean includeOne)Description copied from interface:PcgReturns 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.
- Specified by:
nextFloatin interfacePcg- 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:
Pcg.nextFloat()
-
nextGaussian
public double nextGaussian()
- Specified by:
nextGaussianin interfacePcg- Overrides:
nextGaussianin classjava.util.Random
-
getInc
public long getInc()
Description copied from interface:PcgReturns the internal increment of the congurential generator used by this pcg
-
getState
public long getState()
Description copied from interface:PcgReturns the internal state of the congruential generator used by this pcg
-
setInc
protected void setInc(long increment)
-
setState
protected void setState(long state)
-
split
public <T> T split() throws java.lang.ReflectiveOperationExceptionDescription copied from interface:PcgSplits 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
Pcg.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.- Specified by:
splitin interfacePcg- 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
public <T> T splitDistinct() throws java.lang.ReflectiveOperationExceptionDescription copied from interface:PcgSplits 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
Pcg.split()will return a generator who has an identical state and stream number ensuring that the generated sequence is the same as this generator.- Specified by:
splitDistinctin interfacePcg- 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
public int next(int n)
Description copied from interface:PcgReturns 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.
-
getMult
public long getMult()
Description copied from interface:PcgReturns the internal multiplication of the congurential generator used by this pcg
-
isFast
public boolean isFast()
Description copied from interface:PcgReturn 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
-
getRandomSeed
protected static long getRandomSeed()
-
-