Class PcgRSUFast


  • public class PcgRSUFast
    extends java.lang.Object
    A 64 bit State PcgRNG with 32 bit output. PCG-XSH-RR

    The 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:

     
     ((state >>> 22) ^ state) >>> ((state >>> 61) + 22)
     
     
    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 PcgRSFast is 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 void advance​(long steps)
      Advance or set back the rngs state.
      static long distance​(PcgRS other)
      Calculate the distance of this generator to another RS instance.
      static boolean nextBoolean()
      Returns the next pseudorandom, uniformly distributed boolean value from this random number generator's sequence.
      static boolean nextBoolean​(double probability)  
      static byte nextByte()  
      static void nextBytes​(byte[] b)  
      static char nextChar()  
      static double nextDouble()  
      static double nextDouble​(boolean includeZero, boolean includeOne)  
      static float nextFloat()  
      static float nextFloat​(boolean includeZero, boolean includeOne)  
      static double nextGaussian()  
      static int nextInt()
      Returns the next pseudorandom, uniformly distributed int value from this random number generator's sequence.
      static int nextInt​(int n)
      Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.
      static long nextLong()  
      static long nextLong​(long n)  
      static short nextShort()  
      static void seed​(long seed, long streamNumber)
      Seed the rng with the given seed and stream number.
      static void setStreamConstant​(long constant)
      Deprecated.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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 RNG
        streamNumber - 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 TRUE
         
        Be 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 distributed int value from this random number generator's sequence. The general contract of nextInt is that one int value is pseudorandomly generated and returned. All 232 possible int values are produced with (approximately) equal probability.
        Returns:
        the next pseudorandom, uniformly distributed int value from this random number generator's sequence
      • nextInt

        public static int nextInt​(int n)
        Returns a pseudorandom, uniformly distributed int value 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 int value between zero (inclusive) and bound (exclusive) from this random number generator's sequence
      • nextBoolean

        public static boolean nextBoolean()
        Returns the next pseudorandom, uniformly distributed boolean value from this random number generator's sequence. The general contract of nextBoolean is that one boolean value is pseudorandomly generated and returned. The values true and false are produced with (approximately) equal probability.
        Returns:
        the next pseudorandom, uniformly distributed boolean value 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