dimanche 18 janvier 2015

Random number generation in windows

I have been using the following code for random integer generation when developing my projects on Ubuntu GNU/linux.



class RandomInteger {
std::mt19937 m_gen;
std::random_device m_rd;

public:
RandomInteger() { m_gen = std::mt19937(m_rd()); }

int operator()(int a, int b) {
std::uniform_int_distribution<> dis{std::min(a, b), std::max(a, b)};
return dis(m_gen);
}
};


This generates random numbers when calling operator(). Well at least it's random enough that it seems random. But I was surprised when I compiled the same code in windows, and my program was suddenly acting very deterministically. Turns out this code generates the same numbers every time on windows.


Was I misusing the c++ random number generators all along? Why does it seem random in linux but not in windows? What can I do to make it random on both?





Aucun commentaire:

Enregistrer un commentaire