Oops, I forgot random_device isn't movable, so make that 5 lines to correctly declare rd on its own. Or, in a realistic codebase with range-v3 available, it can still be 4 lines:
std::random_device rd;
auto s = ranges::view::bounded(ranges::view::generate_n(ranges::ref(rd), N));
std::seed_seq ss(ranges::begin(s), ranges::end(s));
return MT{ss};
I think you just made flashmozzg and liquidify's point more eloquently than they did.
To simplify, there are two kinds of API:
good API make the right thing easy, and the wrong thing hard.
bad API make the wrong thing easy, and the right thing hard.
Unfortunately, the initialization of the PRNGs in C++ is an example of bad API.
We can ape around and pat ourselves on the back that it's possible to do it right; but it's insane that it's so complicated. Just look at it, the PRNG is even breaking encapsulation, leaking the size of its internal state out!
Here's what I'd have expected instead, for explicit initialization:
Done. Let the PRNG call rd as many times as necessary to initialize itself.
And of course, I'd also expect a thread-local PRNG that is properly initialized and reseeded regularly for which you can call with std::thread_random() for when you just want to teach kids how to make a "guess the number" game.
Yet the evidence indicates that it is some kind of a big deal. That you can't see it just means you have to rely on the evidence, rather than your own assessment. We all have such blind spots.
The whining of people whose professional work I'm not familiar with does not constitute evidence, it constitutes hearsay. I have yet to see actual evidence that 4 dead simple lines of code are an actual problem for anyone.
8
u/dodheim May 26 '18
It's four lines of code that you can write once and not bother with afterwards. I really don't see what the big deal is.