Class CMWC4096RNG

  • All Implemented Interfaces:
    java.io.Serializable, RepeatableRNG

    public class CMWC4096RNG
    extends java.util.Random
    implements RepeatableRNG

    A Java version of George Marsaglia's Complementary Multiply With Carry (CMWC) RNG. This is a very fast PRNG with an extremely long period (2^131104). It should be used in preference to the MersenneTwisterRNG when a very long period is required.

    One potential drawback of this RNG is that it requires significantly more seed data than the other RNGs provided by Uncommons Maths. It requires just over 16 kilobytes, which may be a problem if your are obtaining seed data from a slow or limited entropy source. In contrast, the Mersenne Twister requires only 128 bits of seed data.

    NOTE: Because instances of this class require 16-kilobyte seeds, it is not possible to seed this RNG using the Random.setSeed(long) method inherited from Random. Calls to this method will have no effect. Instead the seed must be set by a constructor.

    Since:
    1.2
    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static long A  
      private int carry  
      private int index  
      private java.util.concurrent.locks.ReentrantLock lock  
      private byte[] seed  
      private static int SEED_SIZE_BYTES  
      private int[] state  
    • Constructor Summary

      Constructors 
      Constructor Description
      CMWC4096RNG()
      Creates a new RNG and seeds it using the default seeding strategy.
      CMWC4096RNG​(byte[] seed)
      Creates an RNG and seeds it with the specified seed data.
      CMWC4096RNG​(SeedGenerator seedGenerator)
      Seed the RNG using the provided seed generation strategy.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      byte[] getSeed()
      protected int next​(int bits)
      • Methods inherited from class java.util.Random

        doubles, doubles, doubles, doubles, ints, ints, ints, ints, longs, longs, longs, longs, nextBoolean, nextBytes, nextDouble, nextFloat, nextGaussian, nextInt, nextInt, nextLong, setSeed
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • seed

        private final byte[] seed
      • state

        private final int[] state
      • carry

        private int carry
      • index

        private int index
      • lock

        private final java.util.concurrent.locks.ReentrantLock lock
    • Constructor Detail

      • CMWC4096RNG

        public CMWC4096RNG()
        Creates a new RNG and seeds it using the default seeding strategy.
      • CMWC4096RNG

        public CMWC4096RNG​(SeedGenerator seedGenerator)
                    throws SeedException
        Seed the RNG using the provided seed generation strategy.
        Parameters:
        seedGenerator - The seed generation strategy that will provide the seed value for this RNG.
        Throws:
        SeedException - If there is a problem generating a seed.
      • CMWC4096RNG

        public CMWC4096RNG​(byte[] seed)
        Creates an RNG and seeds it with the specified seed data.
        Parameters:
        seed - The seed data used to initialise the RNG.
    • Method Detail

      • getSeed

        public byte[] getSeed()
        Specified by:
        getSeed in interface RepeatableRNG
        Returns:
        The seed data used to initialise this pseudo-random number generator.
      • next

        protected int next​(int bits)
        Overrides:
        next in class java.util.Random