samedi 23 juillet 2016

C++ - Boost : Getting same random number . Seeding does not seem to work

I created a class MyRandomto roll a die on a uniform distribution, given the range as input:

MyRandom.cpp

#include "MyRandom.h"

MyRandom::MyRandom(){
    gen.seed(static_cast<unsigned int>(std::time(0)));
}


int MyRandom::die(int min, int max){

    boost::uniform_int<> dist(min, max);
    boost::variate_generator<boost::mt19937&, boost::uniform_int<> > role(gen, dist);
    int result = role();
    role.engine().seed();
    role.distribution().reset();
    return result;
}

main.cpp

std::cout <<  myRandom.die(0, 8) << std::endl; 
std::cout <<  myRandom.die(0, 8) << std::endl;
std::cout <<  myRandom.die(0, 8) << std::endl;
std::cout <<  myRandom.die(0, 8) << std::endl;
std::cout <<  myRandom.die(0, 8) << std::endl;
std::cout <<  myRandom.die(0, 8) << std::endl;
std::cout <<  myRandom.die(0, 8) << std::endl;

I keep getting the same number (except the first one, so my reset somewhat works). Obviously, I am not seeding this correctly. I tried to add the reset() as suggested here without success. What am I missing?




Aucun commentaire:

Enregistrer un commentaire