I'm using g++ with MinGW in Windows to compile my c++ code, which looks like this:
std::mt19937_64 rng(std::random_device{}());
std::uniform_int_distribution<int> dist(0, words.size() - 1);
std::string curr = words[dist(rng)];
as you can see, this extracts a random word from an array of std::string, but when I optput that word, its always the same, i've realised that the problem lies in std::random_device, which outputs always the same number.
why is this happening? how can I fix it?
gcc version 5.3.0
I use vs code for programming if it could help
And of course i properly include random
You are using std::random_device
on MinGW. More specifically, you are creating a new instance every time you want a random number. This usage is not supported (or rather, does not produce random numbers). See Why do I get the same sequence for every run with std::random_device with mingw gcc4.8.1? for the fact that MinGW has this behavior, but to fix your particular code, you can do something like this:
std::mt19937_64 rng(std::random_device{}());
std::uniform_int_distribution<int> dist(0, words.size() - 1);
for (int ii = 0; ii < 100; ++ii) {
std::string curr = words[dist(rng)];
}
That is, use the same random_device
repeatedly, rather than just once. And consider seeding it, or using a different RNG, if randomness is important between program runs.