A few years ago, I was reading through some code handed to us by the amazing university lecturer Fredrik Lindahl, and there was one line that caught my attention. It was, to me, a totally new way to generate random numbers that were actually somewhat random: Mersenne Twister!
What I had heard before, and many times, was to avoid using the “default” way of generating random numbers (which applies to many popular programming languages).
Python, C/C++, JavaScript, Lua, Go, etc., the default random number generators provided in those languages are not recommended for secure applications, and often should not be used at all. Now why is that?
The simple answer (tl;dr) is that random numbers are mathematically complex, and many random number implementations are flawed in one way or another. Not every developer’s needs are the same, so a seemingly random number could be all that is needed. And now that the simple behaviour is the default, changing it would break applications that rely on it, so the worse random number generators get to stay.
The worst way!
Using C++’s standard rand function to generate random numbers is an old-time classic in my mind; every developer has surely done this at least once in their lifetime as a programmer (in whatever language they started with), and it’s really not a great way to generate a random number…
In the case of C languages, the numbers turn out to be terrible compared to some more modern ones like Rust…
for (int index = 0; index < 100; index++)
{
// Technically, this line should be outside of the
// loop for the best random numbers. But to see how
// it performs in the worst case, we have this inside.
srand(time(NULL) + index);
int number = rand() % 6;
std::cout << number << ", ";
}With the code above, we will get numbers like 1, 3, 4, 1, 3, 4, 6, 3, 4, 6, 3, 4, 6, 1, 4, 6, 1, 4, etc. printed. Notice here that a different seed is being used for every new dice throw, i.e., the random function is being reseeded for every throw.
This should give completely new and unpredictable numbers every time, right? Please tell me we are getting random numbers!!
Yeah no we don’t, not even close. The rand function in C uses one of the simplest random number algorithms, and has existed since 1958. It’s a Linear Congruential Generator, or LCG for short. These do not play nice with the last few bits of the generated number and are, quite frankly, terrible for random numbers.
In the example code, I also tried my best to show the worst side of the rand function, and I’m feeling kind of bad about it. The rand function doesn’t deserve all the hate I’m giving it here since, when seeded only once, it’s actually quite useful! As long as the lowest bits aren’t needed and the starting seed is properly chosen. This does not make it a good choice; it’s just not as bad when used correctly.
The slightly better way
Already since C++11, there has instead been a more robust random number generator that I did not hear about until about a few years ago in university. Looking more deeply into why this new class exists shows that it has strong statistical properties that should basically always be used whenever high-quality random numbers are needed.
for (int i = 0; i < 100; i++)
{
// Technically, this line should be outside of the
// loop for the best random numbers. But to see how
// it performs in the worst case, we have this inside.
std::mt19937 twister{ std::random_device{}() };
std::uniform_int_distribution distribute{ 1, 6 };
std::cout << distribute(twister) << ", ";
}With the same torture test above with the Mersenne Twister, we will instead get something like 6, 6, 3, 2, 2, 4, 5, 3, 5, 6, 4, 4, 3, 4, 3, 5, 6. This looks the same to every human, but plotted over time, this is much better randomness, especially at lower bit counts. And it doesn’t break down even when the constructor gets abused like this.
Why even care?
But then, why should you even care if your random numbers are random-ish or not? It’s not like you will be going around needing random numbers all the time. And that is very true, but would you rather have results that look like the left side or the right side when picking your so-called “random” numbers?


The left image shows LCG’s low-bit issues; the right image shows the benefit of high-quality random numbers, in this case, a Mersenne Twister.
Have a wonderful day,
William Bergh

Leave a Reply