It’s pretty old stuff, but still…
Here’s how you typically generate a random number in C++:
std::random_device rd; std::mt19937 gen(rd()); std::uniform_int_distribution<int> dis(1, 100); auto random_number = dis(gen);
Let’s dissect it a little. On the one hand, “random_device
” is a rather nebulous concept: it may or may not be truly random, and it may or may not be cryptographically secure. Truth be told, it’s completely implementation dependent. On the other hand, “mt19937
” is an awfully specific random number generator implementation. These two creatures from two different worlds are usually used in tandem: the above is a boiler-plate example for generating random numbers in C++.
Seasoned C++ programmers got used to a class named mt19937
, but to the regular folks this resembles a Reddit user name rather than class name. If I saw a class named fb24498
in a pull request, I would reject it, and so would most of my colleagues. But it is somehow OK for the C++ standard. As a user, do I really need to remember the magic number 19937 and what “mt” stands for? Isn’t it, like “multi-threaded” or something? Don’t I have other important things to care about? Of course, I can always use ranlux24
, but it’s hardly better.
Compare it to Python:
import random random_number = random.randint(1,100)
The seeding is done for you automatically, no questions asked. Yes, it can be argued that this is kind of vague, but so is random_device
. C++ version does not add much clarity, but totally kills the ease of use.
How about C# then?
var rng = new Random(); int num1 = rng.Next(1, 100);
Go?
rand.Seed(time.Now().UnixNano()); num := 1+rand.Intn(100);
Nobody but C++ seems to force users to remember magic numbers like 19937…
Permalink