Package pal.math

Class MersenneTwisterFast

java.lang.Object
pal.math.MersenneTwisterFast
All Implemented Interfaces:
Serializable

public class MersenneTwisterFast extends Object implements Serializable
MersenneTwisterFast: A simulation quality fast random number generator (MT19937) with the same public methods as java.util.Random.

About the Mersenne Twister. This is a Java version of the C-program for MT19937: Integer version. next(32) generates one pseudorandom unsigned integer (32bit) which is uniformly distributed among 0 to 2^32-1 for each call. next(int bits) >>>'s by (32-bits) to get a value ranging between 0 and 2^bits-1 long inclusive; hope that's correct. setSeed(seed) set initial values to the working area of 624 words. For setSeed(seed), seed is any 32-bit integer except for 0.

Reference. M. Matsumoto and T. 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.

Bug Fixes. This implementation implements the bug fixes made in Java 1.2's version of Random, which means it can be used with earlier versions of Java. See the JDK 1.2 java.util.Random documentation for further documentation on the random-number generation contracts made. Additionally, there's an undocumented bug in the JDK java.util.Random.nextBytes() method, which this code fixes.

Important Note. Just like java.util.Random, this generator accepts a long seed but doesn't use all of it. java.util.Random uses 48 bits. The Mersenne Twister instead uses 32 bits (int size). So it's best if your seed does not exceed the int range.

Sean Luke's web page

- added shuffling method (Alexei Drummond)

See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructor using the time of day as default seed.
    Constructor using a given seed.
  • Method Summary

    Modifier and Type
    Method
    Description
    static final boolean
    Access a default instance of this class, access is synchronized
    static final byte
    Access a default instance of this class, access is synchronized
    static final void
    getNextBytes(byte[] bs)
    Access a default instance of this class, access is synchronized
    static final char
    Access a default instance of this class, access is synchronized
    static final double
    Access a default instance of this class, access is synchronized
    static final float
    Access a default instance of this class, access is synchronized
    static final int
    Access a default instance of this class, access is synchronized
    static final long
    Access a default instance of this class, access is synchronized
    static final short
    Access a default instance of this class, access is synchronized
    final boolean
     
    final byte
     
    final void
    nextBytes(byte[] bytes)
     
    final char
     
    final double
     
    final float
     
    final double
     
    final int
     
    int
    nextInt(int n)
    Returns an integer drawn uniformly from 0 to n-1.
    final long
     
    final short
     
    final void
    setSeed(long seed)
    Initalize the pseudo random number generator.
    final void
    shuffle(int[] array)
    Shuffles an array.
    final void
    shuffle(int[] array, int numberOfShuffles)
    Shuffles an array by repeatedly choosing two random members and swapping them.
    final void
    shuffle(Object[] array)
    Shuffles an array of objects.
    int[]
    shuffled(int l)
    Generates an array of ints that are shuffled
    final void
    shuffleSubset(int startIndex, int length, Object[] array)
    Shuffles an array of objects.

    Methods inherited from class java.lang.Object

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

    • MersenneTwisterFast

      public MersenneTwisterFast()
      Constructor using the time of day as default seed.
    • MersenneTwisterFast

      public MersenneTwisterFast(long seed)
      Constructor using a given seed. Though you pass this seed in as a long, it's best to make sure it's actually an integer.
      Parameters:
      seed - generator starting number, often the time of day.
  • Method Details

    • shuffle

      public final void shuffle(int[] array)
      Shuffles an array.
      Parameters:
      array - The array of ints to shuffle
    • shuffleSubset

      public final void shuffleSubset(int startIndex, int length, Object[] array)
      Shuffles an array of objects.
      Parameters:
      array - The array of objects to shuffle
      startIndex - the starting index of the portion of the array to shuffle
      length - the length of the portion of the array to shuffle
    • shuffle

      public final void shuffle(Object[] array)
      Shuffles an array of objects.
      Parameters:
      array - The array of objects to shuffle
    • shuffle

      public final void shuffle(int[] array, int numberOfShuffles)
      Shuffles an array by repeatedly choosing two random members and swapping them.
      Parameters:
      numberOfShuffles - The number of times to do the random swap operation
      array - The array of ints to shuffle
    • shuffled

      public int[] shuffled(int l)
      Generates an array of ints that are shuffled
      Parameters:
      l - length of the array required.
      Returns:
      an array of shuffled indices of the specified length.
    • setSeed

      public final void setSeed(long seed)
      Initalize the pseudo random number generator. The Mersenne Twister only uses an integer for its seed; It's best that you don't pass in a long that's bigger than an int.
      Parameters:
      seed - from constructor
    • nextInt

      public final int nextInt()
    • nextShort

      public final short nextShort()
    • nextChar

      public final char nextChar()
    • nextBoolean

      public final boolean nextBoolean()
    • nextByte

      public final byte nextByte()
    • nextBytes

      public final void nextBytes(byte[] bytes)
    • nextLong

      public final long nextLong()
    • nextDouble

      public final double nextDouble()
    • nextGaussian

      public final double nextGaussian()
    • nextFloat

      public final float nextFloat()
    • nextInt

      public int nextInt(int n)
      Returns an integer drawn uniformly from 0 to n-1. Suffice it to say, n must be > 0, or an IllegalArgumentException is raised.
    • getNextByte

      public static final byte getNextByte()
      Access a default instance of this class, access is synchronized
    • getNextBoolean

      public static final boolean getNextBoolean()
      Access a default instance of this class, access is synchronized
    • getNextBytes

      public static final void getNextBytes(byte[] bs)
      Access a default instance of this class, access is synchronized
    • getNextChar

      public static final char getNextChar()
      Access a default instance of this class, access is synchronized
    • getNextDouble

      public static final double getNextDouble()
      Access a default instance of this class, access is synchronized
    • getNextFloat

      public static final float getNextFloat()
      Access a default instance of this class, access is synchronized
    • getNextLong

      public static final long getNextLong()
      Access a default instance of this class, access is synchronized
    • getNextShort

      public static final short getNextShort()
      Access a default instance of this class, access is synchronized
    • getNextInt

      public static final int getNextInt()
      Access a default instance of this class, access is synchronized