0

I need some random numbers for a simulation and are experimenting with the C++ 11 random library using MinGW Distro from nuwen.net.

As have been discussed in several other threads, e.g. Why do I get the same sequence for every run with std::random_device with mingw gcc4.8.1?, random_device do not generate a random seed, i.e. the code below, compiled with GCC, generates the same sequence of numbers for every run.

// test4.cpp
// MinGW Distro - nuwen.net
// Compile with g++ -Wall -std=c++14 test4.cpp -o test4

#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;
}

Run 1: 56 72 34 91 0 59 87 51 95 97 16 66 31 52 70 78

Run 2: 56 72 34 91 0 59 87 51 95 97 16 66 31 52 70 78

To solve this problem it has been suggested to use the boost library, and the code would then look something like below, adopted from How do I use boost::random_device to generate a cryptographically secure 64 bit integer? and from A way change the seed of boost::random in every different program run,

// test5.cpp
// MinGW Distro - nuwen.net
// Compile with g++ -Wall -std=c++14 test5.cpp -o test5

#include <boost/random.hpp>
#include <boost/random/random_device.hpp>
#include <iostream>
#include <random>

using namespace std;

int main(){
    boost::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;
}

But this code wont compile, gives the error “undefined reference to ‘boost::random::random_device::random_device()”. Note that both random.hpp and radndom_device.hpp are available in the include directory. Can anyone suggest what is wrong with the code, or with the compiling?

Community
  • 1
  • 1
John
  • 1,645
  • 2
  • 17
  • 29
  • boost::random is a compiled boost library, in your command line you must link with it to compile this program, or you will get a linker error. – Chris Beck Mar 01 '16 at 06:11
  • Ok, compiling test5.cpp above with g++ -std=c++14 test5.cpp -o test5 E:\MinGW\lib\libboost_random.a E:\MinGW\lib\libboost_system.a works, i.e. produce a list of different random numbers for each run. – John Mar 01 '16 at 07:56

1 Answers1

0

Linking the code to the boost libraries libboost_random.a and libboost_system.a seems to solve the problem, the executable generates of list of different random numbers for each run.

// test5.cpp
// MinGW Distro - nuwen.net
// g++ -std=c++14 test5.cpp -o test5 E:\MinGW\lib\libboost_random.a E:\MinGW\lib\libboost_system.a

#include <boost/random.hpp>
#include <boost/random/random_device.hpp>
#include <iostream>
#include <random>

using namespace std;
int main(){
    boost::random_device rd;
    boost::mt19937 mt(rd());
    uniform_int_distribution<int> dist(0,99);
    for (int i = 0; i< 16; ++i){
        cout<<dist(mt)<<" ";
        }
        cout <<endl;
}

Run 1: 20 89 31 30 74 3 93 43 68 4 64 38 74 37 4 69

Run 2: 40 85 99 72 99 29 95 32 98 73 95 88 37 59 79 66

John
  • 1,645
  • 2
  • 17
  • 29