Mersenne Twister Random Number Generator

The Mersenne Twister is an algorithm for generating random numbers. It was designed with consideration of the flaws in various other generators. The period, 2^19937-1, and the order of equidistribution, 623 dimensions, are far greater. The generator is also fast; it avoids multiplication and division, and it benefits from caches and pipelines. See the inventors' page for more details.

I have implemented the Mersenne Twister in a C++ class that is fast, convenient, portable, and free. Take a look at the class or download the complete package in zip or tarball format.

Features:

On my system, a Pentium III running Linux at 500 MHz, the performance test gives the following results for generation of random integers:

MersenneTwister.h28.4 million per second
Inventors' C version14.3 million per second
Cokus's optimized C version16.6 million per second
Standard rand()6.8 million per second

The latest version, v1.0, incorporates several changes released by the Mersenne Twister inventors on 26 January 2002. The seeding algorithm was revised to correct a minor problem in which the highest bit of the seed was not well represented in the generator state. The ability to start with large seeds was extended to seed arrays of arbitrary length. Access was added for 53-bit real numbers in [0,1), matching the accuracy of IEEE doubles. Also, the software license was changed from the GNU Lesser General Public License to a BSD license, making commercial use of this software more convenient.

The v1.0 release includes some other improvements as well. By popular demand, access was added for real numbers from normal (Gaussian) distributions. Safeguards were added to prevent out-of-range number generation on 64-bit machines. Finally, new optimizations yield 25% faster generation overall and 100% faster generation for integers in [0,n].

^ home
Rick Wagner ( rjwagner@writeme.com ) 15 May 03