Interface Pcg

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.util.concurrent.atomic.AtomicLong UNIQUE_SEED
      Ensure that a unique seed is used for randomly seeded instances
    • Method Summary

      All Methods Instance Methods Abstract Methods Default Methods 
      Modifier and Type Method Description
      void advance​(long steps)
      Advance or set back the rngs state.
      default long distance​(Pcg other)
      Return the distance between the two generators.
      default long distanceUnsafe​(Pcg other)
      Return the distance between the two generators.
      long getInc()
      Returns the internal increment of the congurential generator used by this pcg
      long getMult()
      Returns the internal multiplication of the congurential generator used by this pcg
      long getState()
      Returns the internal state of the congruential generator used by this pcg
      boolean isFast()
      Return true if this rng is a fast instance.
      int next​(int n)
      Returns an integer with the next n low bits randomly set and are used as a base to deviate smaller data types.
      boolean nextBoolean()
      Returns the next pseudorandom, uniformly distributed boolean value from this random number generator's sequence.
      boolean nextBoolean​(double probability)
      Returns the next pseudorandom boolean value from this random number generator's sequence with the given probability of being true.
      byte nextByte()
      Returns the next pseudorandom, uniformly distributed byte value from this random number generator's sequence.
      void nextBytes​(byte[] bytes)
      Generates random bytes and places them into a user-supplied byte array.
      char nextChar()
      Returns the next pseudorandom, uniformly distributed char value from this random number generator's sequence.
      double nextDouble()
      Returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence.
      double nextDouble​(boolean includeZero, boolean includeOne)
      Returns the next pseudorandom, uniformly distributed double value in the range from 0.0 to 1.0, possibly inclusive of 0.0 and 1.0 themselves.
      float nextFloat()
      Returns the next pseudorandom, uniformly distributed float value between 0.0 and 1.0 from this random number generator's sequence.
      float nextFloat​(boolean includeZero, boolean includeOne)
      Returns the next pseudorandom, uniformly distributed float valuet in the range from 0.0f to 1.0f, possibly inclusive of 0.0f and 1.0f themselves.
      double nextGaussian()  
      int nextInt()
      Returns the next pseudorandom, uniformly distributed int value from this random number generator's sequence.
      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.
      long nextLong()
      Returns the next pseudorandom, uniformly distributed long value from this random number generator's sequence.
      long nextLong​(long n)
      Returns the next pseudorandom, uniformly distributed long value from this random number generator's sequence.
      short nextShort()
      Returns the next pseudorandom, uniformly distributed short value from this random number generator's sequence.
      <T> T split()
      Splits the generator in a copy with the exact same state and stream number.
      <T> T splitDistinct()
      Splits the generator in a copy with distinct state and stream number.
    • Field Detail

      • UNIQUE_SEED

        static final java.util.concurrent.atomic.AtomicLong UNIQUE_SEED
        Ensure that a unique seed is used for randomly seeded instances
    • Method Detail

      • advance

        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
      • split

        <T> T split()
             throws java.lang.ReflectiveOperationException
        Splits 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 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.

        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

        <T> T splitDistinct()
                     throws java.lang.ReflectiveOperationException
        Splits 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 split() will return a generator who has an identical state and stream number ensuring that the generated sequence is the same as this generator.

        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

        int next​(int n)
        Returns 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.
        Parameters:
        n - the number of randomly set bits. Must be positive and does not produce reasonable results for > 31
        Returns:
        an integer
      • nextBoolean

        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. All possible boolean values are produced with (approximately) equal probability.
        Returns:
        the next pseudorandom, uniformly distributed boolean value from this random number generator's sequence
      • nextBoolean

        boolean nextBoolean​(double probability)
        Returns the next pseudorandom boolean value from this random number generator's sequence with the given probability of being true.

        A probability value of 0 will always return false and a value of 1 will always return true.

        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
        Throws:
        java.lang.IllegalArgumentException - if probability is > 1 or < 0
      • nextBytes

        void nextBytes​(byte[] bytes)
        Generates 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.
        Parameters:
        bytes - the byte array to fill with random bytes
        Throws:
        java.lang.NullPointerException - if the byte array is null
        See Also:
        nextByte()
      • nextChar

        char nextChar()
        Returns the next pseudorandom, uniformly distributed char value from this random number generator's sequence. The general contract of nextChar is that one char value is pseudorandomly generated and returned. All possible char values are produced with (approximately) equal probability.
        Returns:
        the next pseudorandom, uniformly distributed char value from this random number generator's sequence
      • nextShort

        short nextShort()
        Returns the next pseudorandom, uniformly distributed short value from this random number generator's sequence. The general contract of nextShort is that one short value is pseudorandomly generated and returned. All possible short values are produced with (approximately) equal probability.
        Returns:
        the next pseudorandom, uniformly distributed short value from this random number generator's sequence
      • nextByte

        byte nextByte()
        Returns the next pseudorandom, uniformly distributed byte value from this random number generator's sequence. The general contract of nextByte is that one byte value is pseudorandomly generated and returned. All possible byte values are produced with (approximately) equal probability.
        Returns:
        the next pseudorandom, uniformly distributed byte value from this random number generator's sequence
      • nextInt

        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

        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. The general contract of nextInt is that one int value in the specified range is pseudorandomly generated and returned. All bound possible int values are produced with (approximately) equal probability.
        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
        Throws:
        java.lang.IllegalArgumentException - if bound is not positive
        See Also:
        nextInt()
      • nextLong

        long nextLong()
        Returns the next pseudorandom, uniformly distributed long value from this random number generator's sequence. The general contract of nextLong is that one long value is pseudorandomly generated and returned.
        Returns:
        the next pseudorandom, uniformly distributed long value from this random number generator's sequence
      • nextLong

        long nextLong​(long n)
        Returns the next pseudorandom, uniformly distributed long value from this random number generator's sequence. The general contract of nextLong is that one long value is pseudorandomly generated and returned.
        Parameters:
        n - the upper bound (exclusive). Must be positive.
        Returns:
        the next pseudorandom, uniformly distributed long value from this random number generator's sequence
      • nextFloat

        float nextFloat()
        Returns the next pseudorandom, uniformly distributed float value between 0.0 and 1.0 from this random number generator's sequence.

        The general contract of nextFloat is that one float value, chosen (approximately) uniformly from the range 0.0f (inclusive) to 1.0f (exclusive), is pseudorandomly generated and returned. All 224 possible float values of the form m x 2-24, where m is a positive integer less than 224, are produced with (approximately) equal probability.

        Returns:
        the next pseudorandom, uniformly distributed float value between 0.0 and 1.0 from this random number generator's sequence
      • nextFloat

        float nextFloat​(boolean includeZero,
                        boolean includeOne)
        Returns the next pseudorandom, uniformly distributed float valuet in the range from 0.0f to 1.0f, possibly inclusive of 0.0f and 1.0f themselves. Thus:
        ExpressionInterval
        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.

        Parameters:
        includeZero - if true may return 0f
        includeOne - if true may return 1f
        Returns:
        the next pseudorandom, uniformly distributed float value from this random number generator's sequence
        See Also:
        nextFloat()
      • nextDouble

        double nextDouble()
        Returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence.

        The general contract of nextDouble is that one double value, chosen (approximately) uniformly from the range 0.0d (inclusive) to 1.0d (exclusive), is pseudorandomly generated and returned.

        Returns:
        the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence
      • nextDouble

        double nextDouble​(boolean includeZero,
                          boolean includeOne)
        Returns the next pseudorandom, uniformly distributed double value in the range from 0.0 to 1.0, possibly inclusive of 0.0 and 1.0 themselves. Thus:
        ExpressionInterval
        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.

        Parameters:
        includeZero - if true may return 0d
        includeOne - if true may return 1d
        Returns:
        the next pseudorandom, uniformly distributed double value from this random number generator's sequence
        See Also:
        nextDouble()
      • nextGaussian

        double nextGaussian()
      • distance

        default long distance​(Pcg other)
        Return the distance between the two generators. The distance is the number of steps this generator is ahead or behind the other generator. After advancing or rewinding the amount of steps returned by this function both instances have caught up and will return the same value for primitive nextX() function calls.

        Be aware that this guarantee only holds true for single threaded instances. An exception will be thrown if the comparing objects are not from the same class. If you regardlessly want to compare two generators for which getClass() != other.getClass() is true but may still reach the same state (e.g. a non fast and fast or locked and cas) implementation have a look at distanceUnsafe(Pcg) which does not check for class safety.

        Parameters:
        other - the generator to compare this instance to
        Returns:
        the distance between the two generators
        Throws:
        IncompatibleGeneratorException - if the other generator isn't of the same class as this generator or the increment of the generators is distinct resulting the generators to never be able to reach the same state.
        See Also:
        distanceUnsafe(Pcg)
      • distanceUnsafe

        default long distanceUnsafe​(Pcg other)
        Return the distance between the two generators. The distance is the number of steps this generator is ahead or behind the other generator. After advancing or rewinding the amount of steps returned by this function both instances have caught up and will return the same value for primitive nextX() function calls.

        Be aware that this guarantee only holds true for single threaded instances. This method does Not check if the 2 instances of the generators are of the same class, employ the same algorithm. This may result in undetermined results being returned if the algorithm terminates at all. For a save version take a look at distance(Pcg)

        Parameters:
        other - the generator to compare this instance to
        Returns:
        the distance between the two generators
        Throws:
        IncompatibleGeneratorException - if the increment of the generators is distinct resulting the generators to never be able to reach the same state.
        See Also:
        distance(Pcg)
      • isFast

        boolean isFast()
        Return 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

        long getState()
        Returns the internal state of the congruential generator used by this pcg
        Returns:
        the internal state
      • getInc

        long getInc()
        Returns the internal increment of the congurential generator used by this pcg
        Returns:
        the increment
      • getMult

        long getMult()
        Returns the internal multiplication of the congurential generator used by this pcg
        Returns:
        the multiplication factor