Class RandomBaseLocked
- java.lang.Object
-
- java.util.Random
-
- com.github.kilianB.pcg.RandomBase64
-
- com.github.kilianB.pcg.lock.RandomBaseLocked
-
- All Implemented Interfaces:
Pcg,java.io.Serializable
- Direct Known Subclasses:
PcgRRLocked,PcgRSLocked
public abstract class RandomBaseLocked extends RandomBase64
Extension of the 64 bit state 32 bit output PCG Base. This class synchronizes critical components to ensure thread safety using locks. Lock based implementations shine in high congestion settings but usually have a huge penalty associated with acquiring and releasing the locks in uncontested settings. SeeRandomBase64for more information regarding the internal working of the pcg family- 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 class com.github.kilianB.pcg.RandomBase64
MULT_64
-
Fields inherited from interface com.github.kilianB.pcg.Pcg
UNIQUE_SEED
-
-
Constructor Summary
Constructors Modifier Constructor Description RandomBaseLocked()RandomBaseLocked(long seed, long streamNumber)Create a random number generator with the given seed and stream number.protectedRandomBaseLocked(long initialState, long increment, boolean dummy)Deprecated.
-
Method Summary
All 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 pcglonggetState()Returns the internal state of the congruential generator used by this pcgbooleanisFast()Return true if this rng is a fast instance.protected voidsetInc(long increment)Set the increment of the pcg.protected voidsetState(long initialState)Set the internal state of the pcg.protected longstepRight()Update the state of the lcg and move a step forward.-
Methods inherited from class com.github.kilianB.pcg.RandomBase64
getInt, getMult, getRandomSeed, next, nextBoolean, nextByte, nextBytes, nextChar, nextDouble, nextFloat, nextInt, nextLong, nextLong, nextShort, setSeed, setSeed, split, splitDistinct
-
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
distance, distanceUnsafe, nextBoolean, nextDouble, nextFloat, nextGaussian, nextInt
-
-
-
-
Constructor Detail
-
RandomBaseLocked
public RandomBaseLocked()
-
RandomBaseLocked
public RandomBaseLocked(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 implementations. RNGs with different seeds are able to catch up after they exhaust their period and produce the same numbers.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.
-
RandomBaseLocked
@Deprecated protected RandomBaseLocked(long initialState, long increment, boolean dummy)Deprecated.
-
-
Method Detail
-
stepRight
protected long stepRight()
Description copied from class:RandomBase64Update 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.- Specified by:
stepRightin classRandomBase64- Returns:
- the old value of the state variable before updating.
-
advance
public void advance(long steps)
Description copied from interface:PcgAdvance 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.Pcg.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
-
setInc
protected void setInc(long increment)
Description copied from class:RandomBase64Set 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-
- Specified by:
setIncin classRandomBase64- Parameters:
increment- of the pcg
-
setState
protected void setState(long initialState)
Description copied from class:RandomBase64Set 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-
- Specified by:
setStatein classRandomBase64- Parameters:
initialState- of the 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- Returns:
- true if the subclass uses the newly generated state directly
-
getState
public long getState()
Description copied from interface:PcgReturns the internal state of the congruential generator used by this pcg- Returns:
- the internal state
-
getInc
public long getInc()
Description copied from interface:PcgReturns the internal increment of the congurential generator used by this pcg- Returns:
- the increment
-
-