2

I need to generate uniformly distributed random numbers for a Monte Carlo simulation. I read this thread Random double C++11 and followed the suggestions but, still, I am getting the same number whenever I run the program in Code Blocks. Here is my code:

#include <iostream>  
#include <random>    

int main(){

    std::random_device rd;                              // use random_device to get a random seed
    std::mt19937 mt(rd());                              // mt19937 is a proper pseudo-random number generator 
    std::uniform_real_distribution<double> unif(0,1);   // generate uniformly distributed random doubles in [0,1]

    double x = unif(mt);
    std::cout << x << std::endl;
}

Any suggestion why that happens?

Angelos
  • 169
  • 10
  • 2
    What OS and compiler are you using? – NathanOliver Jun 19 '20 at 21:07
  • Windows 10, GNU GCC compiler and have ticked the box in the compiler settings which says "Have G++ follow the C++11 ISO C++ language standard [-std=c++11]". – Angelos Jun 19 '20 at 21:09
  • Code seems ok https://wandbox.org/permlink/OgT3oSMpYKbKDQS2 – Thomas Sablik Jun 19 '20 at 21:10
  • 4
    `std::random_device` used to be broken in MinGW pre GCC 9.2 (I think). Update your compiler (you can get GCC 10 from MSYS2). – HolyBlackCat Jun 19 '20 at 21:10
  • Whenever you get the same set of random numbers upon program execution beware that it's not a bug but a feature. I.e. you test your app always with the same random numbers, and then on production release you change the seed of the random number generator. Here's how I generate random numbers: ```#include ... std::default_random_engine engine{static_cast(time(0))}; std::uniform_int_distribution randomInt{1, 6}; ... cout< – Hack06 Jun 19 '20 at 21:14
  • 1
    @Hack06 That statement would be true if a specific seed was used that stayed the same between execution. In this case, the seed comes from a `std::random_device`, which one would expect to yield different numbers. – Ken Wayne VanderLinde Jun 19 '20 at 21:18
  • @KenWayneVanderLinde, there must be some hardcoded seed in the STD function implicitly called by the code that OP is using. – Hack06 Jun 19 '20 at 21:20
  • 2
    @Hack06 And that is a bug, not a feature. `std::random_device` should give you a different number every time you use it. It not doing so is a deficiency. The standard allows for this as not all systems have a hardware source of randomness, but it's a compromise that causes issues just like this. – NathanOliver Jun 19 '20 at 21:23
  • 1
    Unfortunately the C++ Standard doesn't [guarantee that expectation](http://eel.is/c++draft/rand#device-2). `random_device` is allowed to return less random information, and MinGW's GCC went and used a [Randall Number Generator](https://xkcd.com/221/), likely to signify "Not working. Don't use." – user4581301 Jun 19 '20 at 21:27
  • https://en.cppreference.com/w/cpp/numeric/random/random_device says **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.** And there's also a note about MinGW known issue. – Hack06 Jun 19 '20 at 21:27

0 Answers0