public class MersenneTwisterFast
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.
- added shuffling method (Alexei Drummond)
public MersenneTwisterFast()
Constructor using the time of day as default seed.
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.
seed - generator starting number, often the time of day.public void shuffle(kotlin.Array[] array)
Shuffles an array.
array - The array of ints to shufflepublic void shuffleSubset(int startIndex,
int length,
java.lang.Object[] array)
Shuffles an array of objects.
array - The array of objects to shufflestartIndex - the starting index of the portion of the array to shufflelength - the length of the portion of the array to shufflepublic void shuffle(java.lang.Object[] array)
Shuffles an array of objects.
array - The array of objects to shufflepublic void shuffle(kotlin.Array[] array,
int numberOfShuffles)
Shuffles an array by repeatedly choosing two random members and swapping them.
numberOfShuffles - The number of times to do the random swap operationarray - The array of ints to shufflepublic kotlin.Array[] shuffled(int l)
Generates an array of ints that are shuffled
l - length of the array required.public 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.
seed - from constructorpublic int nextInt()
public short nextShort()
public char nextChar()
public boolean nextBoolean()
public byte nextByte()
public void nextBytes(kotlin.Array[] bytes)
public long nextLong()
public double nextDouble()
public double nextGaussian()
public float nextFloat()
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.
public static byte getNextByte()
Access a default instance of this class, access is synchronized
public static boolean getNextBoolean()
Access a default instance of this class, access is synchronized
public static void getNextBytes(kotlin.Array[] bs)
Access a default instance of this class, access is synchronized
public static char getNextChar()
Access a default instance of this class, access is synchronized
public static double getNextDouble()
Access a default instance of this class, access is synchronized
public static float getNextFloat()
Access a default instance of this class, access is synchronized
public static long getNextLong()
Access a default instance of this class, access is synchronized
public static short getNextShort()
Access a default instance of this class, access is synchronized
public static int getNextInt()
Access a default instance of this class, access is synchronized