0

I am try to run this code:

#include <iostream>
#include <random>

void foo() {
  std::random_device rd;
  std::mt19937 mt(rd());
  std::uniform_real_distribution<double> prob(0, 1);
  std::cout << prob(mt) << '\n';
}

int main() {
  for (int i = 0; i < 5; ++i) {
    foo();
  }
  return 0;
}

At the mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64 I get this result:

0.726249
0.726249
0.726249
0.726249
0.726249

But on the ideone and g++ I get different numbers. So what the problem?

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
dasfex
  • 1,148
  • 2
  • 12
  • 30
  • 3
    `random_device` used to be broken in MinGW, it was fixed in GCC 9.2. Either update your compiler or seed using `std::time(nullptr)` or something else. One way to get the latest and greatest MinGW GCC is from MSYS2. – HolyBlackCat Sep 20 '20 at 08:51
  • 1
    @HolyBlackCat Even if `std::random_device` is broken and produces the same number each time, it just initializes the state of `mt` and is no longer referenced after construction. Shouldn't the output be 5 differing values but produce the same sequence each time the program is run if only `std::random_device` is broken? – doug Sep 20 '20 at 20:02

0 Answers0