# Random numbers on C++

Recently I got used to modern languages which include a stock good random generator, which usually is the Mersenne Twister; now that I got back to C++ I have to decide what to use.

I searched for Mersenne Twister implementations and I noticed there are so many: is there one which is more used and widespread, or am I supposed to pick one assuming they are all equally good?

• I like your separation of C++ and modern languages. – jcora Mar 17 '12 at 23:14
• Maybe saying "higher level" was more appropriate. – o0'. Mar 17 '12 at 23:27
• I think this question belongs on stackoverflow – TravisG Mar 18 '12 at 8:48
• On SO I'd give a different answer because I wouldn't know it was for a game engine as opposed to, say, Monte Carlo simulations for medical therapies, in which case not having 624 dimensions of randomness can be deadly. – user744 Mar 18 '12 at 8:58

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;

• Yeah, that's the kind of answer I was looking for, that's why I posted here on gamedev : ) – o0'. Mar 18 '12 at 10:34
• Just wanted to note that nothing in the files you linked indicate that they are in the public domain. The way copyright law works, there really needs to be a clear note about that, as the law assumes "all rights reserve" by default. It's actually even safer to just use something like the MIT or BSD 2-clause license, since some jurisdictions basically don't acknowledge "this is in the public domain" as legally binding. If you're interested in seeing people use your code, it might be worth taking care of that. – Sean Middleditch Mar 18 '12 at 23:19
• @seanmiddleditch: I am being clear about it right here. If you want it under a MIT-style license, I'll follow SQLite's lead and give that to you for only \$1000. – user744 Mar 19 '12 at 9:35
• The lack if a header in the code declaring anything (which SQLite does, iirc) is the main problem. If you don't care, that's cool. Was just giving you a friendly suggestion. – Sean Middleditch Mar 19 '12 at 20:40

Another RNG I've used before for gamedev purposes is Bob Jenkins' "small" RNG, described here.

(He also has a cryptographic-strength RNG called ISAAC, but it's bigger and slower, and games don't need that level of strength.)

• That looks more expensive than xorshift (4 xors and 3 shifts vs. 4 adds, 6 shifts, 2 ors, and a xor), has a worse period, and runs the risk of very short cycles with certain initializations. It looks fast but not fastest; okay period but nowhere near optimal; same basic distribution qualities as xorshift; I don't see any reason to use it. – user744 Mar 18 '12 at 20:11
• Fair enough. I don't know enough about RNG analysis to dig into the distribution and cycle properties. – Nathan Reed Mar 18 '12 at 20:43