Class MersenneTwister
- java.lang.Object
-
- cern.colt.PersistentObject
-
- cern.jet.random.engine.RandomEngine
-
- cern.jet.random.engine.MersenneTwister
-
- All Implemented Interfaces:
DoubleFunction
,IntFunction
,Serializable
,Cloneable
public class MersenneTwister extends RandomEngine
MersenneTwister (MT19937) is one of the strongest uniform pseudo-random number generators known so far; at the same time it is quick. Produces uniformly distributedint
's andlong
's in the closed intervals[Integer.MIN_VALUE,Integer.MAX_VALUE]
and[Long.MIN_VALUE,Long.MAX_VALUE]
, respectively, as well asfloat
's anddouble
's in the open unit intervals(0.0f,1.0f)
and(0.0,1.0)
, respectively. The seed can be any 32-bit integer except0
. Shawn J. Cokus commented that perhaps the seed should preferably be odd.Quality: MersenneTwister is designed to pass the k-distribution test. It has an astronomically large period of 219937-1 (=106001) and 623-dimensional equidistribution up to 32-bit accuracy. It passes many stringent statistical tests, including the diehard test of G. Marsaglia and the load test of P. Hellekalek and S. Wegenkittl.
Performance: Its speed is comparable to other modern generators (in particular, as fast as
java.util.Random.nextFloat()
). 2.5 million calls toraw()
per second (Pentium Pro 200 Mhz, JDK 1.2, NT). Be aware, however, that there is a non-negligible amount of overhead required to initialize the data structures used by a MersenneTwister. Code likedouble sum = 0.0; for (int i=0; i<100000; ++i) { RandomElement twister = new MersenneTwister(new java.util.Date()); sum += twister.raw(); }
will be wildly inefficient. Consider usingdouble sum = 0.0; RandomElement twister = new MersenneTwister(new java.util.Date()); for (int i=0; i<100000; ++i) { sum += twister.raw(); }
instead. This allows the cost of constructing the MersenneTwister object to be borne only once, rather than once for each iteration in the loop.Implementation: After 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.
- More info on Masumoto's homepage.
- More info on Pseudo-random number generators is on the Web.
- Yet some more info.
The correctness of this implementation has been verified against the published output sequence mt19937-2.out of the C-implementation mt19937-2.c. (Call
test(1000)
to print the sequence). Note that this implementation is not synchronized.Details: MersenneTwister is designed with consideration of the flaws of various existing generators in mind. It is an improved version of TT800, a very successful generator. MersenneTwister is based on linear recurrences modulo 2. Such generators are very fast, have extremely long periods, and appear quite robust. MersenneTwister produces 32-bit numbers, and every
k
-dimensional vector of such numbers appears the same number of times ask
successive values over the period length, for eachk <= 623
(except for the zero vector, which appears one time less). If one looks at only the firstn <= 16
bits of each number, then the property holds for even largerk
, as shown in the following table (taken from the publication cited above):MersenneTwister generates random numbers in batches of 624 numbers at a time, so the caching and pipelining of modern systems is exploited. The generator is implemented to generate the output by using the fastest arithmetic operations only: 32-bit additions and bit operations (no division, no multiplication, no mod). These operations generate sequences of 32 random bits (
int
's).long
's are formed by concatenating two 32 bitint
's.float
's are formed by dividing the interval[0.0,1.0]
into 232 sub intervals, then randomly choosing one subinterval.double
's are formed by dividing the interval[0.0,1.0]
into 264 sub intervals, then randomly choosing one subinterval.- Version:
- 1.0, 09/24/99
- Author:
- wolfgang.hoschek@cern.ch
- See Also:
Random
, Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static int
DEFAULT_SEED
-
Constructor Summary
Constructors Constructor Description MersenneTwister()
Constructs and returns a random number generator with a default seed, which is a constant.MersenneTwister(int seed)
Constructs and returns a random number generator with the given seed.MersenneTwister(Date d)
Constructs and returns a random number generator seeded with the given date.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Object
clone()
Returns a copy of the receiver; the copy will produce identical sequences.protected void
nextBlock()
Generates N words at one time.int
nextInt()
Returns a 32 bit uniformly distributed random number in the closed interval[Integer.MIN_VALUE,Integer.MAX_VALUE]
(includingInteger.MIN_VALUE
andInteger.MAX_VALUE
).protected void
setSeed(int seed)
Sets the receiver's seed.-
Methods inherited from class cern.jet.random.engine.RandomEngine
apply, apply, makeDefault, nextDouble, nextFloat, nextLong, raw
-
-
-
-
Field Detail
-
DEFAULT_SEED
public static final int DEFAULT_SEED
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
MersenneTwister
public MersenneTwister()
Constructs and returns a random number generator with a default seed, which is a constant. Thus using this constructor will yield generators that always produce exactly the same sequence. This method is mainly intended to ease testing and debugging.
-
MersenneTwister
public MersenneTwister(int seed)
Constructs and returns a random number generator with the given seed.
-
MersenneTwister
public MersenneTwister(Date d)
Constructs and returns a random number generator seeded with the given date.- Parameters:
d
- typicallynew java.util.Date()
-
-
Method Detail
-
clone
public Object clone()
Returns a copy of the receiver; the copy will produce identical sequences. After this call has returned, the copy and the receiver have equal but separate state.- Overrides:
clone
in classPersistentObject
- Returns:
- a copy of the receiver.
-
nextBlock
protected void nextBlock()
Generates N words at one time.
-
nextInt
public int nextInt()
Returns a 32 bit uniformly distributed random number in the closed interval[Integer.MIN_VALUE,Integer.MAX_VALUE]
(includingInteger.MIN_VALUE
andInteger.MAX_VALUE
).- Specified by:
nextInt
in classRandomEngine
-
setSeed
protected void setSeed(int seed)
Sets the receiver's seed. This method resets the receiver's entire internal state.
-
-