C++11 includes a Mersenne Twister generator by default as part of its new <random>
interface. For example, to generate integers uniformly between [-10, 10] using MT:
std::mt19937 eng; // This is the Mersenne Twister
std:::uniform_int_distribution<int> dist(-10, 10)
for (int i = 0; i < 10; ++i)
std::cout << dist(eng) << std::endl;
Most of this is also available in any compiler offering TR1 though the names are slightly different; std::tr1::mt19937
and std::tr1::uniform_int<int>
.
I usually caution people away from using Mersenne Twister. It's an okay algorithm but a lot of its popularity is just marketing. 624 dimensions of randomness is more than most people need, and MT carries relatively heavy state requirements and when it does a full table recalc it can blow cache. I am personally partial to xorshift which gives excellent periods and reasonable distributions for anything a game needs, with tiny memory and CPU requirements.
I've written a (mostly?) C++11-compliant xorshift generator - xorshift.hpp, xorshift.cpp - and placed it in the public domain. You can plug this into any C++11 randomization function, as above:
xorshift eng;
std:::uniform_int_distribution<int> dist(-10, 10)
for (int i = 0; i < 10; ++i)
std::cout << dist(eng) << std::endl;