Class PcgRSUFast
- java.lang.Object
-
- com.github.kilianB.pcg.fast.PcgRSUFast
-
public class PcgRSUFast extends java.lang.ObjectA 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. All methods are made static to achieve an even higher performance but leaves this class at a questionable state. For almost all cases((state >>> 22) ^ state) >>> ((state >>> 61) + 22)PcgRSFastis a much more suited implementation.- Author:
- Kilian
- See Also:
- www.pcg-random.org
-
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static voidadvance(long steps)Advance or set back the rngs state.static longdistance(PcgRS other)Calculate the distance of this generator to another RS instance.static booleannextBoolean()Returns the next pseudorandom, uniformly distributedbooleanvalue from this random number generator's sequence.static booleannextBoolean(double probability)static bytenextByte()static voidnextBytes(byte[] b)static charnextChar()static doublenextDouble()static doublenextDouble(boolean includeZero, boolean includeOne)static floatnextFloat()static floatnextFloat(boolean includeZero, boolean includeOne)static doublenextGaussian()static intnextInt()Returns the next pseudorandom, uniformly distributedintvalue from this random number generator's sequence.static intnextInt(int n)Returns a pseudorandom, uniformly distributedintvalue between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.static longnextLong()static longnextLong(long n)static shortnextShort()static voidseed(long seed, long streamNumber)Seed the rng with the given seed and stream number.static voidsetStreamConstant(long constant)Deprecated.
-
-
-
Method Detail
-
seed
public static void seed(long seed, long streamNumber)Seed the rng 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.
-
advance
public static 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
-
nextByte
public static byte nextByte()
-
nextBytes
public static void nextBytes(byte[] b)
-
nextChar
public static char nextChar()
-
nextShort
public static short nextShort()
-
nextInt
public static 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
public static 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.- 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
-
nextBoolean
public static 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.- Returns:
- the next pseudorandom, uniformly distributed
booleanvalue from this random number generator's sequence
-
nextBoolean
public static boolean nextBoolean(double probability)
-
nextLong
public static long nextLong()
-
nextLong
public static long nextLong(long n)
-
nextDouble
public static double nextDouble()
-
nextDouble
public static double nextDouble(boolean includeZero, boolean includeOne)
-
nextFloat
public static float nextFloat()
-
nextFloat
public static float nextFloat(boolean includeZero, boolean includeOne)
-
nextGaussian
public static double nextGaussian()
-
setStreamConstant
@Deprecated public static void setStreamConstant(long constant)
Deprecated.
-
distance
public static long distance(PcgRS other)
Calculate the distance of this generator to another RS instance. The distance is defined in the numbers of steps one generator has to perform to catch up and produce the same results as the other generator.- Parameters:
other- the generator to compare this state to- Returns:
- the distance between the two generators
-
-