Performance: Its speed is comparable to other modern generators (in particular, as fast as java.util.Random.nextFloat()). 2.5 million calls to raw() 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 like
double 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 using
double 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.
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).
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 as k successive values over the period length, for each k <= 623 (except for the zero vector, which appears one time less). If one looks at only the first n <= 16 bits of each number, then the property holds for even larger k, as shown in the following table (taken from the publication cited above):
n | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 .. 16 | 17 .. 32 |
k | 19937 | 9968 | 6240 | 4984 | 3738 | 3115 | 2493 | 2492 | 1869 | 1869 | 1248 | 1246 | 623 |
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 bit int'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.
@author wolfgang.hoschek@cern.ch @version 1.0, 09/24/99 @see java.util.Random
This generator is described in a paper by Makoto Matsumoto and Takuji Nishimura in 1998: 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
This class is mainly a Java port of the 2002-01-26 version of the generator written in C by Makoto Matsumoto and Takuji Nishimura. Here is their original copyright:
Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, All rights reserved. |
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This generator is described in a paper by Makoto Matsumoto and Takuji Nishimura in 1998: 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
This class is mainly a Java port of the 2002-01-26 version of the generator written in C by Makoto Matsumoto and Takuji Nishimura. Here is their original copyright:
Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, All rights reserved. |
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
The Mersenne Twister is a very high-quality PRNG specifically suited for stochastic sampling and simulation. It has very high-order of dimensional equidistribution and passes even the Diehard tests. The period of this algorithm is 219937 − 1, so repetition is pretty far away. That said it is also pretty fast in average, except for the first number to be generated and if the internal pool runs out. It is not recommend for cryptographic purposes! @author Johannes Rössel
|
|
|
|
|
|
|
|
|
|