Class RandomBase64
- java.lang.Object
-
- java.util.Random
-
- com.github.kilianB.pcg.RandomBase64
-
- All Implemented Interfaces:
Pcg,java.io.Serializable
- Direct Known Subclasses:
RandomBaseCAS,RandomBaseLocked,RandomBaseSynchonized
public abstract class RandomBase64 extends java.util.Random implements Pcg
Base class for 64 bit state pcg random number generators with 32 bit output. 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.
Contract: every extending class must implement a copy constructor with a signature of(long,long,boolean). As it does not perform proper initialization of the seed this method should not be exposed.
- Author:
- Kilian
- See Also:
- www.pcg-random.org, Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected static longMULT_64Linear congruential constant.-
Fields inherited from interface com.github.kilianB.pcg.Pcg
UNIQUE_SEED
-
-
Constructor Summary
Constructors Modifier Constructor Description RandomBase64()Seeds the generator with 2 longs generated by xorshift*.RandomBase64(long seed, long streamNumber)Create a random number generator with the given seed and stream number.protectedRandomBase64(long initialState, long increment, boolean dummy)Deprecated.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected abstract intgetInt(long state)Construct a 32bit int from the given 64bit state using a permutation function.longgetMult()Returns the internal multiplication of the congurential generator used by this pcgprotected static longgetRandomSeed()Return a random 64 bit seed ensuring uniqueness by using a xorshift64* shift algorithm.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(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(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(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.intnextInt()Returns the next pseudorandom, uniformly distributedintvalue 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 abstract voidsetInc(long newInc)Set the increment of the pcg.voidsetSeed(long seed)Deprecated.This method behaves differently than you would expect from the random base class.voidsetSeed(long seed, long streamNumber)Sets the seed of this random number generator using .protected abstract voidsetState(long newState)Set the internal state of the pcg.<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.protected abstract longstepRight()Update the state of the lcg and move a step forward.-
Methods inherited from class java.util.Random
doubles, doubles, doubles, doubles, ints, ints, ints, ints, longs, longs, longs, longs, nextBoolean, nextDouble, nextFloat, nextGaussian, nextInt
-
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
advance, distance, distanceUnsafe, getInc, getState, isFast, nextBoolean, nextDouble, nextFloat, nextGaussian, nextInt
-
-
-
-
Field Detail
-
MULT_64
protected static final long MULT_64
Linear congruential constant. Same as MMIX by Donald Knuth and Newlib, Musl- See Also:
- Constant Field Values
-
-
Constructor Detail
-
RandomBase64
public RandomBase64()
Seeds the generator with 2 longs generated by xorshift*. The values choosen are very likely not used in any other invocation of this constructor.
-
RandomBase64
public RandomBase64(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.
-
RandomBase64
@Deprecated protected RandomBase64(long initialState, long increment, boolean dummy)Deprecated.Copy constructor. Has to be implemented in all inheriting instances. This will be invoked through reflection! when callingsplit()orsplitDistinct()If no special behavior is desired simply pass though the values. This constructor should usually not be called manually as the seed and increment will just be set without performing any randomization.- Parameters:
initialState- of the lcg. The value will be set and not altered.increment- used in the lcg. has to be odddummy- unused. Resolve signature disambiguate
-
-
Method Detail
-
setSeed
public void setSeed(long seed)
Deprecated.This method behaves differently than you would expect from the random base class.Sets the seed of this random number generator using a single long seed. 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.Be aware that seeding this random number generation requires 2 arguments. A seed and a stream number.
Calling this method is equivalent to
RandomBase64(long, long); Confusion may arise when constructing a rng instance with a given seed and a different stream number and expecting the rngs to be in the same state as right after the constructor call.In this case please refer to the method
setSeed(long, long);- Overrides:
setSeedin classjava.util.Random- Since:
- 1.0.1
-
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.
-
stepRight
protected abstract long stepRight()
Update the state of the lcg and move a step forward. The old state should be used to extract bits used to construct a number.When implementations use the newly generate state to calculate random numbers
Pcg.isFast()has to return true.- Returns:
- the old value of the state variable before updating.
-
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.
-
getInt
protected abstract int getInt(long state)
Construct a 32bit int from the given 64bit state using a permutation function. The produced int will be used to construct all other datatypes returned by this RNG.- Parameters:
state- random int as produced by the internal lcg- Returns:
- a random int with randomly set bits
-
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
-
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[] bytes)
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:
bytes- 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()
Description copied from interface:PcgReturns 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.
-
nextLong
public 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.The long value is composed of two integers and a 64 bit state therefore, returning all possible long values with equal probability.
-
nextLong
public 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.The long value is composed of two integers and a 64 bit state therefore, returning all possible long values with 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()
-
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()
-
getRandomSeed
protected static long getRandomSeed()
Return a random 64 bit seed ensuring uniqueness by using a xorshift64* shift algorithm. This implementation is thread safe.- Returns:
- a unique seed
-
getMult
public long getMult()
Description copied from interface:PcgReturns the internal multiplication of the congurential generator used by this pcg
-
setState
protected abstract void setState(long newState)
Set the internal state of the pcg. This method is used during the seeding process of this class and therefore, it is most likely is never correct to alter the variable passed to this function.Allowed operations are synchronization on those methods-
- Parameters:
newState- of the pcg
-
setInc
protected abstract void setInc(long newInc)
Set the increment of the pcg. This method is used during the seeding process of this class and therefore, it is most likely is never correct to alter the variable passed to this function."Although there are rules for the choice of constants [17], if we pick a power-of-two modulus and a good multiplicative constant, the only constraint on c for a full period generator is that c is odd and > 0"
Chapter 4.2.1 (http://www.pcg-random.org/pdf/hmc-cs-2014-0905.pdf)
Allowed operations are synchronization on those methods-
- Parameters:
newInc- of the pcg
-
-