Class RandomBaseSynchonized

  • All Implemented Interfaces:
    Pcg, java.io.Serializable
    Direct Known Subclasses:
    PcgRR, PcgRS

    public abstract class RandomBaseSynchonized
    extends RandomBase64
    Extension of the 64 bit state 32 bit output PCG Base. This class synchronizes critical components to ensure thread safety using synchronized monitors. This implementations shines in single threaded and normal congestion situation due to good support by the compiler and should generally be the preferred class to extend from when implementing new rngs.

    See RandomBase64 for 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 long inc
      Stream number of the rng.
      protected long state
      64 bit internal state
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
        RandomBaseSynchonized()  
        RandomBaseSynchonized​(long seed, long streamNumber)
      Create a random number generator with the given seed and stream number.
      protected RandomBaseSynchonized​(long initialState, long increment, boolean dummy)
      Deprecated.
    • Field Detail

      • state

        protected long state
        64 bit internal state
      • inc

        protected long inc
        Stream number of the rng.
    • Constructor Detail

      • RandomBaseSynchonized

        public RandomBaseSynchonized()
      • RandomBaseSynchonized

        public RandomBaseSynchonized​(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 RNG
        streamNumber - used to compute the increment for the lcg.
      • RandomBaseSynchonized

        @Deprecated
        protected RandomBaseSynchonized​(long initialState,
                                        long increment,
                                        boolean dummy)
        Deprecated.
    • Method Detail

      • stepRight

        protected long stepRight()
        Description copied from class: RandomBase64
        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.

        Specified by:
        stepRight in class RandomBase64
        Returns:
        the old value of the state variable before updating.
      • advance

        public void advance​(long steps)
        Description copied from interface: Pcg
        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. 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
      • 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
        Overrides:
        split in class RandomBase64
        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
        Overrides:
        splitDistinct in class RandomBase64
        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
      • distanceUnsafe

        public long distanceUnsafe​(Pcg other)
        Description copied from interface: Pcg
        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 Pcg.distance(Pcg)

        Parameters:
        other - the generator to compare this instance to
        Returns:
        the distance between the two generators
        See Also:
        Pcg.distance(Pcg)
      • getState

        public long getState()
        Description copied from interface: Pcg
        Returns the internal state of the congruential generator used by this pcg
        Returns:
        the internal state
      • getInc

        public long getInc()
        Description copied from interface: Pcg
        Returns the internal increment of the congurential generator used by this pcg
        Returns:
        the increment
      • setInc

        protected void setInc​(long increment)
        Description copied from class: RandomBase64
        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-

        Specified by:
        setInc in class RandomBase64
        Parameters:
        increment - of the pcg
      • setState

        protected void setState​(long initialState)
        Description copied from class: RandomBase64
        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-

        Specified by:
        setState in class RandomBase64
        Parameters:
        initialState - of the pcg
      • isFast

        public boolean isFast()
        Description copied from interface: Pcg
        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