Getting the same random double number at each run in C++ [duplicate] - c++

I use the following code to test the C++ <random> library.
Why do I get the exact same sequence for every run of the compiled executable? Is rd() deterministic upon compilation? How do I get different output for each run?
GCC 4.8.1 on Windows 7 64bit. Using MinGW distribution from http://nuwen.net/mingw.html.
EDIT: I tested the same piece code with Visual Studio. There is no problem. The outputs are non deterministic. This could be a bug in mingw gcc 4.8.1 that I used.
#include <iostream>
#include <random>
using namespace std;
int main(){
random_device rd;
mt19937 mt(rd());
uniform_int_distribution<int> dist(0,99);
for (int i = 0; i< 16; ++i){
cout<<dist(mt)<<" ";
}
cout <<endl;
}

From http://en.cppreference.com/w/cpp/numeric/random/random_device:
Note that std::random_device may be implemented in terms of a pseudo-random number engine if a non-deterministic source (e.g. a hardware device) is not available to the implementation.
I would expect a decent implementation to at least seed the RNG though.
Edit: I suspect they deliberately chose to deliver the same sequence each time, to make obvious the fact that the stream wasn't as random as promised.

I got a confirmed answer from STL from MSFT:
Unlike VC, GCC hasn't implemented random_device nondeterministically on Windows. Boost has, so you can use Boost.Random.

This is a GCC bug, fixed in GCC 9.2.
If you have this problem, update your compiler. (You can get a fresh GCC from MSYS2, for example.)

You may need to pass a parameter to the constructor:
https://gcc.gnu.org/onlinedocs/gcc-4.9.1/libstdc++/api/a00899.html

GCC does not implement rd.entropy() correctly - it always returns 0 (at least on Mac OS X).
Unfortunately, there seems to be no way to mix additional entropy into random_device, which matters because it usually/often (look at Linux /dev/random and /dev/urandom, and at the Intel RDRAND implementation) implements a pseudo-random number generator under the hood. I'd like to be able to improve its output by injecting something I consider random to mix with whatever its entropy source produces. Again, since this device (or kernel module) internally implements a cryptographic algorithm for processing the entropy bits it obtains to generate its output, I'd like to be able to "randomize" that process more by injecting my own data to mix with whatever entropy that device picks.
For example, consider Java SecureRandom(). It does not allow you to set the seed (which indeed would convert it to PRNG), but it would happily mix what you provide with whatever it is using to "randomize" its output even more.
I personally prefer RDRAND. A small assembly library with a compact C interface. Here are the references:
David Johnson from Intel explains RDRAND on Stackoverflow
Stackoverflow pointers to RDRAND library source for Windows, Linux, and Mac OS X
Intel blog on RDRAND library, and a download link

Related

mt19937 returns same number even though it is seeded [duplicate]

I use the following code to test the C++ <random> library.
Why do I get the exact same sequence for every run of the compiled executable? Is rd() deterministic upon compilation? How do I get different output for each run?
GCC 4.8.1 on Windows 7 64bit. Using MinGW distribution from http://nuwen.net/mingw.html.
EDIT: I tested the same piece code with Visual Studio. There is no problem. The outputs are non deterministic. This could be a bug in mingw gcc 4.8.1 that I used.
#include <iostream>
#include <random>
using namespace std;
int main(){
random_device rd;
mt19937 mt(rd());
uniform_int_distribution<int> dist(0,99);
for (int i = 0; i< 16; ++i){
cout<<dist(mt)<<" ";
}
cout <<endl;
}
From http://en.cppreference.com/w/cpp/numeric/random/random_device:
Note that std::random_device may be implemented in terms of a pseudo-random number engine if a non-deterministic source (e.g. a hardware device) is not available to the implementation.
I would expect a decent implementation to at least seed the RNG though.
Edit: I suspect they deliberately chose to deliver the same sequence each time, to make obvious the fact that the stream wasn't as random as promised.
I got a confirmed answer from STL from MSFT:
Unlike VC, GCC hasn't implemented random_device nondeterministically on Windows. Boost has, so you can use Boost.Random.
This is a GCC bug, fixed in GCC 9.2.
If you have this problem, update your compiler. (You can get a fresh GCC from MSYS2, for example.)
You may need to pass a parameter to the constructor:
https://gcc.gnu.org/onlinedocs/gcc-4.9.1/libstdc++/api/a00899.html
GCC does not implement rd.entropy() correctly - it always returns 0 (at least on Mac OS X).
Unfortunately, there seems to be no way to mix additional entropy into random_device, which matters because it usually/often (look at Linux /dev/random and /dev/urandom, and at the Intel RDRAND implementation) implements a pseudo-random number generator under the hood. I'd like to be able to improve its output by injecting something I consider random to mix with whatever its entropy source produces. Again, since this device (or kernel module) internally implements a cryptographic algorithm for processing the entropy bits it obtains to generate its output, I'd like to be able to "randomize" that process more by injecting my own data to mix with whatever entropy that device picks.
For example, consider Java SecureRandom(). It does not allow you to set the seed (which indeed would convert it to PRNG), but it would happily mix what you provide with whatever it is using to "randomize" its output even more.
I personally prefer RDRAND. A small assembly library with a compact C interface. Here are the references:
David Johnson from Intel explains RDRAND on Stackoverflow
Stackoverflow pointers to RDRAND library source for Windows, Linux, and Mac OS X
Intel blog on RDRAND library, and a download link

Why does repeatedly running a simple snippet generating pseudo-random numbers always yield the same results? [duplicate]

I use the following code to test the C++ <random> library.
Why do I get the exact same sequence for every run of the compiled executable? Is rd() deterministic upon compilation? How do I get different output for each run?
GCC 4.8.1 on Windows 7 64bit. Using MinGW distribution from http://nuwen.net/mingw.html.
EDIT: I tested the same piece code with Visual Studio. There is no problem. The outputs are non deterministic. This could be a bug in mingw gcc 4.8.1 that I used.
#include <iostream>
#include <random>
using namespace std;
int main(){
random_device rd;
mt19937 mt(rd());
uniform_int_distribution<int> dist(0,99);
for (int i = 0; i< 16; ++i){
cout<<dist(mt)<<" ";
}
cout <<endl;
}
From http://en.cppreference.com/w/cpp/numeric/random/random_device:
Note that std::random_device may be implemented in terms of a pseudo-random number engine if a non-deterministic source (e.g. a hardware device) is not available to the implementation.
I would expect a decent implementation to at least seed the RNG though.
Edit: I suspect they deliberately chose to deliver the same sequence each time, to make obvious the fact that the stream wasn't as random as promised.
I got a confirmed answer from STL from MSFT:
Unlike VC, GCC hasn't implemented random_device nondeterministically on Windows. Boost has, so you can use Boost.Random.
This is a GCC bug, fixed in GCC 9.2.
If you have this problem, update your compiler. (You can get a fresh GCC from MSYS2, for example.)
You may need to pass a parameter to the constructor:
https://gcc.gnu.org/onlinedocs/gcc-4.9.1/libstdc++/api/a00899.html
GCC does not implement rd.entropy() correctly - it always returns 0 (at least on Mac OS X).
Unfortunately, there seems to be no way to mix additional entropy into random_device, which matters because it usually/often (look at Linux /dev/random and /dev/urandom, and at the Intel RDRAND implementation) implements a pseudo-random number generator under the hood. I'd like to be able to improve its output by injecting something I consider random to mix with whatever its entropy source produces. Again, since this device (or kernel module) internally implements a cryptographic algorithm for processing the entropy bits it obtains to generate its output, I'd like to be able to "randomize" that process more by injecting my own data to mix with whatever entropy that device picks.
For example, consider Java SecureRandom(). It does not allow you to set the seed (which indeed would convert it to PRNG), but it would happily mix what you provide with whatever it is using to "randomize" its output even more.
I personally prefer RDRAND. A small assembly library with a compact C interface. Here are the references:
David Johnson from Intel explains RDRAND on Stackoverflow
Stackoverflow pointers to RDRAND library source for Windows, Linux, and Mac OS X
Intel blog on RDRAND library, and a download link

How do I get a random seed for a random int range in C++11? [duplicate]

I use the following code to test the C++ <random> library.
Why do I get the exact same sequence for every run of the compiled executable? Is rd() deterministic upon compilation? How do I get different output for each run?
GCC 4.8.1 on Windows 7 64bit. Using MinGW distribution from http://nuwen.net/mingw.html.
EDIT: I tested the same piece code with Visual Studio. There is no problem. The outputs are non deterministic. This could be a bug in mingw gcc 4.8.1 that I used.
#include <iostream>
#include <random>
using namespace std;
int main(){
random_device rd;
mt19937 mt(rd());
uniform_int_distribution<int> dist(0,99);
for (int i = 0; i< 16; ++i){
cout<<dist(mt)<<" ";
}
cout <<endl;
}
From http://en.cppreference.com/w/cpp/numeric/random/random_device:
Note that std::random_device may be implemented in terms of a pseudo-random number engine if a non-deterministic source (e.g. a hardware device) is not available to the implementation.
I would expect a decent implementation to at least seed the RNG though.
Edit: I suspect they deliberately chose to deliver the same sequence each time, to make obvious the fact that the stream wasn't as random as promised.
I got a confirmed answer from STL from MSFT:
Unlike VC, GCC hasn't implemented random_device nondeterministically on Windows. Boost has, so you can use Boost.Random.
This is a GCC bug, fixed in GCC 9.2.
If you have this problem, update your compiler. (You can get a fresh GCC from MSYS2, for example.)
You may need to pass a parameter to the constructor:
https://gcc.gnu.org/onlinedocs/gcc-4.9.1/libstdc++/api/a00899.html
GCC does not implement rd.entropy() correctly - it always returns 0 (at least on Mac OS X).
Unfortunately, there seems to be no way to mix additional entropy into random_device, which matters because it usually/often (look at Linux /dev/random and /dev/urandom, and at the Intel RDRAND implementation) implements a pseudo-random number generator under the hood. I'd like to be able to improve its output by injecting something I consider random to mix with whatever its entropy source produces. Again, since this device (or kernel module) internally implements a cryptographic algorithm for processing the entropy bits it obtains to generate its output, I'd like to be able to "randomize" that process more by injecting my own data to mix with whatever entropy that device picks.
For example, consider Java SecureRandom(). It does not allow you to set the seed (which indeed would convert it to PRNG), but it would happily mix what you provide with whatever it is using to "randomize" its output even more.
I personally prefer RDRAND. A small assembly library with a compact C interface. Here are the references:
David Johnson from Intel explains RDRAND on Stackoverflow
Stackoverflow pointers to RDRAND library source for Windows, Linux, and Mac OS X
Intel blog on RDRAND library, and a download link

Random number generation in windows [duplicate]

I use the following code to test the C++ <random> library.
Why do I get the exact same sequence for every run of the compiled executable? Is rd() deterministic upon compilation? How do I get different output for each run?
GCC 4.8.1 on Windows 7 64bit. Using MinGW distribution from http://nuwen.net/mingw.html.
EDIT: I tested the same piece code with Visual Studio. There is no problem. The outputs are non deterministic. This could be a bug in mingw gcc 4.8.1 that I used.
#include <iostream>
#include <random>
using namespace std;
int main(){
random_device rd;
mt19937 mt(rd());
uniform_int_distribution<int> dist(0,99);
for (int i = 0; i< 16; ++i){
cout<<dist(mt)<<" ";
}
cout <<endl;
}
From http://en.cppreference.com/w/cpp/numeric/random/random_device:
Note that std::random_device may be implemented in terms of a pseudo-random number engine if a non-deterministic source (e.g. a hardware device) is not available to the implementation.
I would expect a decent implementation to at least seed the RNG though.
Edit: I suspect they deliberately chose to deliver the same sequence each time, to make obvious the fact that the stream wasn't as random as promised.
I got a confirmed answer from STL from MSFT:
Unlike VC, GCC hasn't implemented random_device nondeterministically on Windows. Boost has, so you can use Boost.Random.
This is a GCC bug, fixed in GCC 9.2.
If you have this problem, update your compiler. (You can get a fresh GCC from MSYS2, for example.)
You may need to pass a parameter to the constructor:
https://gcc.gnu.org/onlinedocs/gcc-4.9.1/libstdc++/api/a00899.html
GCC does not implement rd.entropy() correctly - it always returns 0 (at least on Mac OS X).
Unfortunately, there seems to be no way to mix additional entropy into random_device, which matters because it usually/often (look at Linux /dev/random and /dev/urandom, and at the Intel RDRAND implementation) implements a pseudo-random number generator under the hood. I'd like to be able to improve its output by injecting something I consider random to mix with whatever its entropy source produces. Again, since this device (or kernel module) internally implements a cryptographic algorithm for processing the entropy bits it obtains to generate its output, I'd like to be able to "randomize" that process more by injecting my own data to mix with whatever entropy that device picks.
For example, consider Java SecureRandom(). It does not allow you to set the seed (which indeed would convert it to PRNG), but it would happily mix what you provide with whatever it is using to "randomize" its output even more.
I personally prefer RDRAND. A small assembly library with a compact C interface. Here are the references:
David Johnson from Intel explains RDRAND on Stackoverflow
Stackoverflow pointers to RDRAND library source for Windows, Linux, and Mac OS X
Intel blog on RDRAND library, and a download link

Why do I get the same sequence for every run with std::random_device with mingw gcc4.8.1?

I use the following code to test the C++ <random> library.
Why do I get the exact same sequence for every run of the compiled executable? Is rd() deterministic upon compilation? How do I get different output for each run?
GCC 4.8.1 on Windows 7 64bit. Using MinGW distribution from http://nuwen.net/mingw.html.
EDIT: I tested the same piece code with Visual Studio. There is no problem. The outputs are non deterministic. This could be a bug in mingw gcc 4.8.1 that I used.
#include <iostream>
#include <random>
using namespace std;
int main(){
random_device rd;
mt19937 mt(rd());
uniform_int_distribution<int> dist(0,99);
for (int i = 0; i< 16; ++i){
cout<<dist(mt)<<" ";
}
cout <<endl;
}
From http://en.cppreference.com/w/cpp/numeric/random/random_device:
Note that std::random_device may be implemented in terms of a pseudo-random number engine if a non-deterministic source (e.g. a hardware device) is not available to the implementation.
I would expect a decent implementation to at least seed the RNG though.
Edit: I suspect they deliberately chose to deliver the same sequence each time, to make obvious the fact that the stream wasn't as random as promised.
I got a confirmed answer from STL from MSFT:
Unlike VC, GCC hasn't implemented random_device nondeterministically on Windows. Boost has, so you can use Boost.Random.
This is a GCC bug, fixed in GCC 9.2.
If you have this problem, update your compiler. (You can get a fresh GCC from MSYS2, for example.)
You may need to pass a parameter to the constructor:
https://gcc.gnu.org/onlinedocs/gcc-4.9.1/libstdc++/api/a00899.html
GCC does not implement rd.entropy() correctly - it always returns 0 (at least on Mac OS X).
Unfortunately, there seems to be no way to mix additional entropy into random_device, which matters because it usually/often (look at Linux /dev/random and /dev/urandom, and at the Intel RDRAND implementation) implements a pseudo-random number generator under the hood. I'd like to be able to improve its output by injecting something I consider random to mix with whatever its entropy source produces. Again, since this device (or kernel module) internally implements a cryptographic algorithm for processing the entropy bits it obtains to generate its output, I'd like to be able to "randomize" that process more by injecting my own data to mix with whatever entropy that device picks.
For example, consider Java SecureRandom(). It does not allow you to set the seed (which indeed would convert it to PRNG), but it would happily mix what you provide with whatever it is using to "randomize" its output even more.
I personally prefer RDRAND. A small assembly library with a compact C interface. Here are the references:
David Johnson from Intel explains RDRAND on Stackoverflow
Stackoverflow pointers to RDRAND library source for Windows, Linux, and Mac OS X
Intel blog on RDRAND library, and a download link