public class MersenneTwisterFast extends Object implements Serializable
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)
Constructor and Description |
---|
MersenneTwisterFast()
Constructor using the time of day as default seed.
|
MersenneTwisterFast(long seed)
Constructor using a given seed.
|
Modifier and Type | Method and Description |
---|---|
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.
|
public MersenneTwisterFast()
public MersenneTwisterFast(long seed)
seed
- generator starting number, often the time of day.public final void shuffle(int[] array)
public final void shuffle(int[] array, int numberOfShuffles)
public int[] shuffled(int l)
l
- length of the array required.public final void setSeed(long seed)
seed
- from constructorpublic final int nextInt()
public final short nextShort()
public final char nextChar()
public final boolean nextBoolean()
public final byte nextByte()
public final void nextBytes(byte[] bytes)
public final long nextLong()
public final double nextDouble()
public final double nextGaussian()
public final float nextFloat()
public int nextInt(int n)
public static final byte getNextByte()
public static final boolean getNextBoolean()
public static final void getNextBytes(byte[] bs)
public static final char getNextChar()
public static final double getNextDouble()
public static final float getNextFloat()
public static final long getNextLong()
public static final short getNextShort()
public static final int getNextInt()
Copyright © 2015–2021 Fiji. All rights reserved.