I am working on randomized algorithm in C++ in which I need to generate random numbers, both real and integer. I have written two functions:
double random_real_number() {
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<> dis(0.0, 1.0);
double number = dis(gen);
cout << number << endl;
return number;
}
int random_integer_number() {
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<int> dis(0, INT_MAX);
int number = dis(gen);
cout << number << endl;
return number;
}
The functions are called multiple times in a loop by other function. When I compile and run the the program with Visual Studio 2013 everything works fine, but when I use g++ (on Windows) to compile and run the program, the random_real_number()
always produces 0.726249
and random_integer_number()
always produces 14959223606
. I think I have written functions correctly according to opinions shared on internet, but I do not know how to overcome this. I will be thankful for help.