lundi 29 février 2016

MinGW boost random_device compile error

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 same sequence for everyrun 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?

Aucun commentaire:

Enregistrer un commentaire