Modification to Mersenne Twister(MT) Pseudorandom Number Generator

The commonly used Pseudorandom Number Generator (PRNG) in C/C++ is a linear congruential generator. It is not suitable for a Monte Carlo simulation due to its poor-quality randomness, short period, etc. The rand() function in Matlab is much better than the ones in C library, but still presents a strong nonrandom pattern (link).

Recently a new PRNG called Mersenne Twister is widely used in many applications and included in many packages, such as Boost C++ library, or latest version of Matlab. It has very large period and good randomness.

The original C implementation of Mersenne Twister is not thread-safe. Here I provide a modification which makes the code thread-safe. The source code can be freely used, including commercial purpose. It can be download mtrnd.h. A sample usage would be:

#include <mtrnd.h>
MT::MersenneTwist mtrnd;
mtrnd.init_genrand(5489UL); //initialize the Mersenne Twister.
int n = static_cast<int>(floor(mtrnd.genrand_res53()*Nmax));
...