3 ms·
Oh look, yet another person seeding mt19937 poorly. I see them like clockwork. There was a lot of good discussion about this the last time I mentioned it to som
by Veedrac 9y ago
Oh look, yet another person seeding mt19937 poorly. I see them like clockwork. There was a lot of good discussion about this the last time I mentioned it to someone completely different, so here's that before I copy-paste it: https://www.reddit.com/r/programming/comments/6e10b8/when_random_numbers_are_too_random_low/di6upbd/?context=3 https://www.reddit.com/r/programming/comments/6e10b8/when_ra...
---
Your code
thread_local static std::random_device rd{};
thread_local static std::mt19937 engine{rd()};
does not properly seed a mt19937.
It is unfortunate how people keep trying to use `std::mt19937` and, unsurprisingly given how hard it is to use, how they (almost)[0] inevitably fail. Do yourself a favour and use a randomness library that doesn't hate you[1], is simple to seed correctly, produces decent quality randomness, isn't horribly bloated and has more features to boot. It's 2017[2]. Let the Mersenne Twister die.
[0]: https://codereview.stackexchange.com/q/114066/40768 https://codereview.stackexchange.com/q/114066/40768
[1]: http://www.pcg-random.org/ http://www.pcg-random.org/ or http://xoroshiro.di.unimi.it/ http://xoroshiro.di.unimi.it/
[2]: Well, late 2014 in the article, but close enough.
- petters 9y agoThanks for this comment. I learned something!
- rocqua 9y agoFor the lazy reader: The issue is that rd() returns at most a 64-bit int, whilst mt19937 has 19937 bits of state. This means you are very restricted. Note that, for example linux's /dev/random uses 4069 bits of entropy.
- mturmon 9y agoI notice that the PCG paper, while seemingly drafted in 2015 or earlier, still has not appeared in ACM TOMS. I thought the jokey/insider-y style is kind of fun, but VERY different from the usual voice of the journal. I wonder what the cause of the delay is?
- deleted 9y ago[deleted]