Hi! i’ve been learning c++ now for a bit and wanted to explore better (more random) random number generators in c++. Came across the mt19937 generator and after reading about its standard practice use i wanted to try some DND rng test code. However after running my code in vsc, it kept printing the same output - 12, 6, 3 respectively - no matter how i change the code. Is my code wrong? Am i missing some pathing? Do i need to download a specific extension for c++ vsc? Code below.
cout << Thanks!;
#include #include #include #include
using namespace std;
Your code looked correct to me. So i tried compiling and running myself and it works fine. I know this sounds like did you turn it off and on again? but, did you try saving and re-compiling before running it? I know I often forget this lolll.
Yeah i’ve made sure to save and recompile and everything - I even came close to giving it the old Hollywood smack on the top of the computer but thought better of it…
Im gonna keep looking into it - its definitely working elsewhere so its something to do with my local i guess. Will be back when i figure it out (or give it the Hollywood try)!
Figure out what compiler you’re using and your computer’s architecture and OS and you might have better luck in searching around for quirks with random device and those.
So, turns out the compilers based on the port for windows/microsoft (MingW, MinGW-64) actually have a problem with the std::random_device and bug out from time to time. Best case solution is to use a different compiler (still need to look into that a bit more).
However - a nice short term solution to initialize the code correctly is to seed with:
std::random_device rd;
std::mt19937 gen(std::chrono::high_resolution_clock::now().time_since_epoch().count());
That did the trick nicely - be sure to #inlcude chrono
On reading into it a bit further, theres lots more discussion on the pros and cons of simply including higher resolution seed that is all a little above my head at the time. But we’ll be back to investigate that in a bit. In the meantime the code above works! Now time to try a new friggin compiler… Thanks!!
also for what its worth i found this and hope to look into it as well - an alt lib for rng:
"CSPRNG is a small library that uses your Operating System’s native Cryptographically-Secure Pseudo-Random Number Generator.
It is a superior replacement for the clunky, ill-designed [std::random_device]. It won’t throw. It won’t give you non-CS random numbers. It won’t pretend to tell you anything about entropy. It just works."