public class MersenneTwister extends java.lang.Object implements RandomNumber
This is a Java version of the C-program for MT19937: Integer version. genrand() generates one pseudorandom unsigned integer (32bit) which is uniformly distributed among 0 to 2^32-1 for each call. sgenrand(seed) set initial values to the working area of 624 words. Before genrand(), sgenrand(seed) must be called once. (seed is any 32-bit integer except for 0). Orignally Coded by Takuji Nishimura, considering the suggestions by Topher Cooper and Marc Rieffel in July-Aug. 1997. More information can be found at http://www.math.keio.ac.jp/matumoto/emt.html.
Translated to Java by Michael Lecuyer January 30, 1999 Copyright (C) 1999 Michael Lecuyer
Adapted to conform to the RandomNumber
interface in Simkit by
Arnie Buss, 8 October 2002. None of the code was substantially changed,
simply shoved around a bit. For example, filling the int buffer was broken
out to a separate method. Since the RandomNumber interface has methods like
setSeed(long), getSeed(), and resetSeed(), there was a desire to make these
work in a reasonable manner. setSeed(long) and resetSeed() appear to be
working, but there is something I am not getting about "what" the seed is, so
getSeed() does not appear to be correct. The following code
rng.setSeed(rng.getSeed());
should leave the object essentially
in the same state, and it does not.
Simkit is also copyleft under the GNU General Public License.
This library is free software; you can redistribute it and or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Makoto Matsumoto and Takuji Nishimura, the original authors ask "When you use this, send an email to: matumoto@math.keio.ac.jp with an appropriate reference to your work" You might also point out this was a translation.
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.
Modifier and Type | Field and Description |
---|---|
static java.util.logging.Logger |
logger |
private static int |
LOWER_MASK |
private static int |
M |
private int[] |
mag01 |
private static int |
MATRIX_A |
private static double |
MODULUS_MULT |
private int[] |
mt |
private int |
mti |
private static int |
N |
private int |
originalSeed |
private static int |
TEMPERING_MASK_B |
private static int |
TEMPERING_MASK_C |
private static int |
UMASK |
private static int |
UPPER_MASK |
Constructor and Description |
---|
MersenneTwister()
Constructor using the default seed.
|
Modifier and Type | Method and Description |
---|---|
double |
draw()
Generates the next random number and casts to
double . |
long |
drawLong()
Generates the next random number.
|
private void |
fill() |
double |
getMultiplier()
Returns the value for this RandomNumber needed to scale
a number produced by drawLong to result in U(0,1).
|
long |
getSeed()
This does not work properly and should not be relied on.
|
long[] |
getSeeds()
Gets the current (not original) value of the seeds for this RandomNumber.
|
static void |
main(java.lang.String[] args)
This main() outputs first 1000 generated numbers
|
void |
resetSeed()
Resets seed to last setSeed() value
|
void |
setSeed(long seed)
Sets the seed to the given value.
|
void |
setSeeds(long[] seed)
Sets the seed to the value in the first element of the array
|
private void |
sgenrand(int seed)
Initializes the pseudo random number generator.
|
java.lang.String |
toString()
Returns the name of this RandomNumber.
|
public static final java.util.logging.Logger logger
private static final int N
private static final int M
private static final int MATRIX_A
private static final int UPPER_MASK
private static final int LOWER_MASK
private static final double MODULUS_MULT
private static final int TEMPERING_MASK_B
private static final int TEMPERING_MASK_C
private static final int UMASK
private int[] mt
private int mti
private int[] mag01
private int originalSeed
private void sgenrand(int seed)
seed
- from constructorpublic long drawLong()
drawLong
in interface RandomNumber
private void fill()
public double draw()
double
.draw
in interface RandomNumber
public long getSeed()
getSeed
in interface RandomNumber
public long[] getSeeds()
RandomNumber
getSeeds
in interface RandomNumber
public void resetSeed()
resetSeed
in interface RandomNumber
public void setSeed(long seed)
setSeed
in interface RandomNumber
seed
- The new random number seedjava.lang.IllegalArgumentException
- if the seed is 0public void setSeeds(long[] seed)
setSeeds
in interface RandomNumber
seed
- The new array of seedsjava.lang.IllegalArgumentException
- If the array does not contain at least
one element.public java.lang.String toString()
toString
in class java.lang.Object
public static void main(java.lang.String[] args)
args
- the command line argumentspublic double getMultiplier()
RandomNumber
getMultiplier
in interface RandomNumber