I have a short piece of code which runs the Mersenne Twister PRNG and it works great:
std::random_device randDev;
std::mt19937 twister(randDev());
std::uniform_int_distribution<int> dist(0,99);
for (int i = 0; i < 10; i++) {
std::cout << dist(twister) << std::endl;
}
It outputs ten random numbers. However if I put the exact same code into a function:
#include <random>
int getRand(const int& A, const int& B) {
std::random_device randDev;
std::mt19937 twister(randDev());
std::uniform_int_distribution<int> dist(A,B);
return dist(twister);
}
int main() {
for (int i = 0; i < 10; i++) {
std::cout << getRand(0,99) << std::endl;
}
return 0;
}
It outputs the same number ten times. I'm just starting out with C++ so I have no idea what causes this or how to go about fixing the problem.
Aucun commentaire:
Enregistrer un commentaire