pal.math
Class MersenneTwisterFast

java.lang.Object
  |
  +--pal.math.MersenneTwisterFast
All Implemented Interfaces:
java.io.Serializable

public class MersenneTwisterFast
extends java.lang.Object
implements java.io.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:
Serialized Form

Constructor Summary
MersenneTwisterFast()
          Constructor using the time of day as default seed.
MersenneTwisterFast(long seed)
          Constructor using a given seed.
 
Method Summary
static boolean getNextBoolean()
          Access a default instance of this class, access is synchronized
static byte getNextByte()
          Access a default instance of this class, access is synchronized
static void getNextBytes(byte[] bs)
          Access a default instance of this class, access is synchronized
static char getNextChar()
          Access a default instance of this class, access is synchronized
static double getNextDouble()
          Access a default instance of this class, access is synchronized
static float getNextFloat()
          Access a default instance of this class, access is synchronized
static int getNextInt()
          Access a default instance of this class, access is synchronized
static long getNextLong()
          Access a default instance of this class, access is synchronized
static short getNextShort()
          Access a default instance of this class, access is synchronized
 boolean nextBoolean()
           
 byte nextByte()
           
 void nextBytes(byte[] bytes)
           
 char nextChar()
           
 double nextDouble()
           
 float nextFloat()
           
 double nextGaussian()
           
 int nextInt()
           
 int nextInt(int n)
          Returns an integer drawn uniformly from 0 to n-1.
 long nextLong()
           
 short nextShort()
           
 void setSeed(long seed)
          Initalize the pseudo random number generator.
 void shuffle(int[] array)
          Shuffles an array.
 void shuffle(int[] array, int numberOfShuffles)
          Shuffles an array.
 int[] shuffled(int l)
          Returns an array of shuffled indices of length l.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

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 Detail

shuffle

public final void shuffle(int[] array)
Shuffles an array.

shuffle

public final void shuffle(int[] array,
                          int numberOfShuffles)
Shuffles an array. Shuffles numberOfShuffles times

shuffled

public int[] shuffled(int l)
Returns an array of shuffled indices of length l.
Parameters:
l - length of the array required.

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