Why Do I Get the Same Sequence For Every Run With Std::Random_Device With Mingw Gcc4.8.1

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

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.

std::random - On Windows always I get the same numbers

The problem here is that the std::random_device object (which you are using to seed your std::mt19937) may produce the same seed, each time (although it doesn't on my Windows 10 + Visual Studio test platform).

From cppreference (bolding mine):

std::random_device may be implemented in terms of an
implementation-defined pseudo-random number engine if a
non-deterministic source (e.g. a hardware device) is not available to
the implementation. In this case each std::random_device object may
generate the same number sequence
.

Here's a possible solution using the 'classic' call to time(nullptr) as the seed, which also avoids the use of the 'intermediate' std::random_device object to generate that seed (though there will be other options to get that seed):

#include <iostream>
#include <random>
#include <ctime>

int main()
{
std::mt19937 gen(static_cast<unsigned int>(time(nullptr)));
std::uniform_int_distribution<> distribut(1, 6);
for (int i = 0; i < 10; i++) {
std::cout << distribut(gen) << ' ';
}
return 0;
}

std::mt19937 generator returns the same values each execution, why? [duplicate]

https://en.cppreference.com/w/cpp/numeric/random/random_device

std::random_device may be implemented in terms of an implementation-defined pseudo-random number engine if a non-deterministic source (e.g. a hardware device) is not available to the implementation. In this case each std::random_device object may generate the same number sequence.

So, apparently your implementation of std::random_device is pseudo-random.
Pass something better then instead of rd() here if you can:

std::mt19937 gen(rd());

Random Device member class gives same sequence?

The problem is the random_device that is not copyable.

There are 3 solution:

  • Static Variable : you can define a static variable in the Ant class.
  • Singleton : you can define a singleton variable for the random_device
  • Global random_device : you can define a global variable for the random_device

The solution is not trivial, but you can adopt one of these above solution, as well as you programming style concerns.

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

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.

C++ 11 random number generation not working [duplicate]

The issue here is that std::random_device does not have to really be a random device. It can be a wrapper around an unseeded rand which would give you the same value every time you use it. This means your seed for engine would be the same which means the pseudo-random sequence it generates would be the same as well.

One way you could get around this is to use the current as a seed like

auto seed = std::chrono::system_clock::now().time_since_epoch().count();
mt19937 engine {seed};

But this can be manipulated via external processes and isn't very fined grained so multiple instances seeded at the same time could all make the same sequence.

Why is random library producing the same results every time when using std::uniform_int_distribution

It seems to be a known problem of g++ implementation on Windows. See the accepted answer to a similar question: Why do I get the same sequence for every run with std::random_device with mingw gcc4.8.1?

To avoid the problem you can use <chrono> facilities to seed the random number generator:

#include <iostream>
#include <random>
#include <cstdlib>
#include <ctime>
#include <chrono>

int main() {
srand(time(0));
long long seed = rand();

std::default_random_engine rand_num{static_cast<long unsigned int>(std::chrono::high_resolution_clock::now().time_since_epoch().count())};
// ^ ^ ^ ^ ^ ^^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
std::uniform_int_distribution<long long> range{1,10};

long long a = range(rand_num);
long long b = rand_num();
std::cout<<seed<<"\n"; // the seed is different every time (tested)
std::cout<<a<<"\n";
std::cout<<b<<"\n";
system("pause");
return 0;
}

This gave me different results at every run ( with g++ on Windows).



Related Topics



Leave a reply



Submit