Class MersenneTwisterFast
- All Implemented Interfaces:
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)
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionConstructor using the time of day as default seed.MersenneTwisterFast
(long seed) Constructor using a given seed. -
Method Summary
Modifier and TypeMethodDescriptionstatic final boolean
Access a default instance of this class, access is synchronizedstatic final byte
Access a default instance of this class, access is synchronizedstatic final void
getNextBytes
(byte[] bs) Access a default instance of this class, access is synchronizedstatic final char
Access a default instance of this class, access is synchronizedstatic final double
Access a default instance of this class, access is synchronizedstatic final float
Access a default instance of this class, access is synchronizedstatic final int
Access a default instance of this class, access is synchronizedstatic final long
Access a default instance of this class, access is synchronizedstatic final short
Access a default instance of this class, access is synchronizedfinal boolean
final byte
nextByte()
final void
nextBytes
(byte[] bytes) final char
nextChar()
final double
final float
final double
final int
nextInt()
int
nextInt
(int n) Returns an integer drawn uniformly from 0 to n-1.final long
nextLong()
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
Shuffles an array of objects.int[]
shuffled
(int l) Generates an array of ints that are shuffledfinal void
shuffleSubset
(int startIndex, int length, Object[] array) Shuffles an array of objects.
-
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
Shuffles an array of objects.- Parameters:
startIndex
- the starting index of the portion of the array to shufflelength
- the length of the portion of the array to shufflearray
- The array of objects to shuffle
-
shuffle
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:
array
- The array of ints to shufflenumberOfShuffles
- The number of times to do the random swap operation
-
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
-