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:
MTRand r;
double a = r();
/dev/urandom
or time()
and clock()
On my system, a Pentium III running Linux at 500 MHz, the performance test gives the following results for generation of random integers:
MersenneTwister.h | 28.4 million per second |
Inventors' C version | 14.3 million per second |
Cokus's optimized C version | 16.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].
Rick Wagner ( rjwagner@writeme.com ) 15 May 03 |