Class MersenneTwister

java.lang.Object
smile.math.random.MersenneTwister
All Implemented Interfaces:
RandomNumberGenerator

public class MersenneTwister extends Object implements RandomNumberGenerator
32-bit Mersenne Twister. This implements the MT19937 (Mersenne Twister) pseudo random number generator algorithm based upon the original C code by Makoto Matsumoto and Takuji Nishimura ( http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html).

As a subclass of java.util.Random this class provides a single canonical method next() for generating bits in the pseudo random number sequence. The user should invoke the public inherited methods (nextInt(), nextFloat() etc.) to obtain values as usual. This class should provide a drop-in replacement for the standard implementation of java.util.Random with the additional advantage of having a far longer period and the ability to use a far larger seed value.

References

  1. Makato Matsumoto and Takuji Nishimura, "Mersenne Twister: A 623-Dimensionally Equidistributed Uniform Pseudo-Random Number Generator", ACM Transactions on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3--30.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructor.
    MersenneTwister(int seed)
    Constructor.
    MersenneTwister(long seed)
    Constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    next(int numbits)
    Returns up to 32 random bits.
    double
    Returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence.
    void
    nextDoubles(double[] d)
    Returns a vector of pseudorandom, uniformly distributed double values between 0.0 and 1.0 from this random number generator's sequence.
    int
    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
    Returns the next pseudorandom, uniformly distributed long value from this random number generator's sequence.
    void
    setSeed(int seed)
    Sets the seed of random numbers.
    void
    setSeed(int[] seed)
    Sets the seed of random numbers.
    void
    setSeed(long seed)
    Initialize the random generator with a seed.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • MersenneTwister

      public MersenneTwister()
      Constructor.
    • MersenneTwister

      public MersenneTwister(int seed)
      Constructor.
      Parameters:
      seed - the seed of random numbers.
    • MersenneTwister

      public MersenneTwister(long seed)
      Constructor.
      Parameters:
      seed - the seed of random numbers.
  • Method Details

    • setSeed

      public void setSeed(long seed)
      Description copied from interface: RandomNumberGenerator
      Initialize the random generator with a seed.
      Specified by:
      setSeed in interface RandomNumberGenerator
      Parameters:
      seed - the seed of random numbers.
    • setSeed

      public void setSeed(int seed)
      Sets the seed of random numbers.
      Parameters:
      seed - the seed of random numbers.
    • setSeed

      public void setSeed(int[] seed)
      Sets the seed of random numbers.
      Parameters:
      seed - the seed of random numbers.
    • next

      public int next(int numbits)
      Description copied from interface: RandomNumberGenerator
      Returns up to 32 random bits.
      Specified by:
      next in interface RandomNumberGenerator
      Parameters:
      numbits - the number of random bits to generate.
      Returns:
      random bits.
    • nextDouble

      public double nextDouble()
      Description copied from interface: RandomNumberGenerator
      Returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence.
      Specified by:
      nextDouble in interface RandomNumberGenerator
      Returns:
      random number.
    • nextDoubles

      public void nextDoubles(double[] d)
      Description copied from interface: RandomNumberGenerator
      Returns a vector of pseudorandom, uniformly distributed double values between 0.0 and 1.0 from this random number generator's sequence.
      Specified by:
      nextDoubles in interface RandomNumberGenerator
      Parameters:
      d - the output random numbers.
    • nextInt

      public int nextInt()
      Description copied from interface: RandomNumberGenerator
      Returns the next pseudorandom, uniformly distributed int value from this random number generator's sequence.
      Specified by:
      nextInt in interface RandomNumberGenerator
      Returns:
      random number.
    • nextInt

      public int nextInt(int n)
      Description copied from interface: RandomNumberGenerator
      Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.
      Specified by:
      nextInt in interface RandomNumberGenerator
      Parameters:
      n - the upper bound of random number (exclusive).
      Returns:
      random number.
    • nextLong

      public long nextLong()
      Description copied from interface: RandomNumberGenerator
      Returns the next pseudorandom, uniformly distributed long value from this random number generator's sequence.
      Specified by:
      nextLong in interface RandomNumberGenerator
      Returns:
      random number.