I have implemented the Mersenne Twister in a C++ class that is fast, convenient, portable, and free. Take a look at the class and example program. Or download the complete package in zip or tarball format.
Features:
MTRand r;
double a = r();
/dev/urandom
or time()
and clock()
The included performance tests verify the speed and correctness of this generator compared to known sequences and alternative implementations.
It has been tested in a variety of hardware and software environments including:
Intel Core 2 Quad, AMD Opteron 848;
Red Hat Linux, Mac OS X 10.5 Leopard, Windows XP;
GNU Compiler Collection 4.2 (g++
), Intel Compiler 10.1 (icpc
), Portland Group International Compiler 9.0 (pgCC
), Pathscale Compiler 3.1 (pathCC
), and Microsoft Visual C++ 2008.
Version 1.0 incorporated 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 included 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 yielded 25% faster generation overall and 100% faster generation for integers in [0,n].
Version 1.1 focuses on portability and reliability. Instances of deprecated C++ features were modernized. Operations were rewritten to work on ones'-complement machines and environments with unsigned enumerations. The test program was toughened and expanded. A slight correction was made in terminology; the parameter for setting the standard deviation of a Gaussian distribution is now properly labeled as "stddev" rather than "variance". A copy constructor and copy operator were also added. The seed correlations, generator functions, output sequences, and save formats are identical to Version 1.0.
Downloads:
Version 1.1 | Mersenne-1.1.zip | Mersenne-1.1.tar.gz |
Version 1.0 | Mersenne-1.0.zip | Mersenne-1.0.tar.gz |
Version 0.8 | Mersenne-0.8.zip | Mersenne-0.8.tar.gz |
Version 0.7 | Mersenne-0.7.zip | Mersenne-0.7.tar.gz |
Version 0.6 | Mersenne-0.6.zip | Mersenne-0.6.tar.gz |
Version 0.5 | Mersenne-0.5.zip | Mersenne-0.5.tar.gz |
Version 0.4 | Mersenne-0.4.zip | Mersenne-0.4.tar.gz |
Version 0.3 | Mersenne-0.3.zip | Mersenne-0.3.tar.gz |
Version 0.2 | Mersenne-0.2.zip | Mersenne-0.2.tar.gz |
Version 0.1 | Mersenne-0.1.zip | Mersenne-0.1.tar.gz |
Comments, questions, and improvements are always welcome.
If you like this software, you might also want to try my ConfigFile (C++) configuration file reader and Anneal.py (Python) module for optimization by simulated annealing.
Creations of Rick Wagner (wagnerr@umich.edu) 30 September 2009