Class 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 long MULT_64
      Linear congruential constant.
    • 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.
      protected RandomBase64​(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 int getInt​(long state)
      Construct a 32bit int from the given 64bit state using a permutation function.
      long getMult()
      Returns the internal multiplication of the congurential generator used by this pcg
      protected static long getRandomSeed()
      Return a random 64 bit seed ensuring uniqueness by using a xorshift64* shift algorithm.
      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​(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​(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​(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.
      int nextInt()
      Returns the next pseudorandom, uniformly distributed int value 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.
      protected abstract void setInc​(long newInc)
      Set the increment of the pcg.
      void setSeed​(long seed)
      Deprecated.
      This method behaves differently than you would expect from the random base class.
      void setSeed​(long seed, long streamNumber)
      Sets the seed of this random number generator using .
      protected abstract void setState​(long newState)
      Set the internal state of the pcg.
      <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.
      protected abstract long stepRight()
      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
    • 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 RNG
        streamNumber - 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 calling split() or splitDistinct() 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 odd
        dummy - 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:
        setSeed in class java.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 RNG
        streamNumber - 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.ReflectiveOperationException
        Description copied from interface: Pcg
        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 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:
        split in interface Pcg
        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.ReflectiveOperationException
        Description copied from interface: Pcg
        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 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:
        splitDistinct in interface Pcg
        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: Pcg
        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.
        Specified by:
        next in interface Pcg
        Overrides:
        next in class java.util.Random
        Parameters:
        n - the number of randomly set bits. Must be positive and does not produce reasonable results for > 31
        Returns:
        an integer
      • 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: Pcg
        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.

        Specified by:
        nextBoolean in interface Pcg
        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: Pcg
        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.
        Specified by:
        nextByte in interface Pcg
        Returns:
        the next pseudorandom, uniformly distributed byte value from this random number generator's sequence
      • nextBytes

        public void nextBytes​(byte[] bytes)
        Description copied from interface: Pcg
        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.
        Specified by:
        nextBytes in interface Pcg
        Overrides:
        nextBytes in class java.util.Random
        Parameters:
        bytes - the byte array to fill with random bytes
        See Also:
        Pcg.nextByte()
      • nextChar

        public char nextChar()
        Description copied from interface: Pcg
        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.
        Specified by:
        nextChar in interface Pcg
        Returns:
        the next pseudorandom, uniformly distributed char value from this random number generator's sequence
      • nextShort

        public short nextShort()
        Description copied from interface: Pcg
        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.
        Specified by:
        nextShort in interface Pcg
        Returns:
        the next pseudorandom, uniformly distributed short value from this random number generator's sequence
      • nextInt

        public int nextInt()
        Description copied from interface: Pcg
        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.
        Specified by:
        nextInt in interface Pcg
        Overrides:
        nextInt in class java.util.Random
        Returns:
        the next pseudorandom, uniformly distributed int value from this random number generator's sequence
      • nextLong

        public 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.

        The long value is composed of two integers and a 64 bit state therefore, returning all possible long values with equal probability.

        Specified by:
        nextLong in interface Pcg
        Overrides:
        nextLong in class java.util.Random
        Returns:
        the next pseudorandom, uniformly distributed long value from this random number generator's sequence
      • nextLong

        public 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.

        The long value is composed of two integers and a 64 bit state therefore, returning all possible long values with equal probability.

        Specified by:
        nextLong in interface Pcg
        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

        public float nextFloat​(boolean includeZero,
                               boolean includeOne)
        Description copied from interface: Pcg
        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.

        Specified by:
        nextFloat in interface Pcg
        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:
        Pcg.nextFloat()
      • nextDouble

        public double nextDouble​(boolean includeZero,
                                 boolean includeOne)
        Description copied from interface: Pcg
        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.

        Specified by:
        nextDouble in interface Pcg
        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:
        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: Pcg
        Returns the internal multiplication of the congurential generator used by this pcg
        Specified by:
        getMult in interface Pcg
        Returns:
        the multiplication factor
      • 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