vendredi 23 octobre 2015

Mingw random number generator generator [duplicate]

I have problem on MinGW 4.8.1-4 using random number generator.

class RandomGenInt
{
    std::random_device rd;
    std::mt19937 gen;
    std::uniform_int_distribution<d_type::Buint> i_dis;
public:
    RandomGenInt(d_type::Bint min,d_type::Bint max)
    {
    gen.seed(rd());
i_dis=std::uniform_int_distribution<d_type::Buint>(min,max);
    }
    d_type::Buint generateRandomInt()
    {

        return i_dis(gen);
    }

};
class RandomGenFloat
{

    std::random_device rd;
    std::mt19937 gen;
    std::uniform_real_distribution<d_type::Bfloat> f_dis;
public:
    RandomGenFloat(d_type::Buint min=0,d_type::Buint max=1000000000)
    {
    gen.seed(rd());
f_dis=std::uniform_real_distribution<d_type::Bfloat>(min,max);

    }
    d_type::Bfloat generateRandomFloat()
    {

        return f_dis(gen);
    }
};

this functions always return the same numbers.

On Linux using gcc the problem doesn't occures... so... what should I do to generate number in c++ on Windows?

EDIT

It all works fine.. I was so stupid that I didn't read the specyfications of std::random_device and std::mt19937 gen . So I have done something like this:

#include <random>
class RandomSampleGenerator: public ISampleGenerator
{
public:
    RandomSampleGenerator();
    Vector2Bf * generateSamples(d_type::Bsize count);

    virtual ~RandomSampleGenerator();
protected:
private:
    std::random_device rd;

};

RandomSampleGenerator.cpp

Vector2Bf* RandomSampleGenerator::generateSamples(d_type::Bsize count)
{


    Vector2Bf* samples = new Vector2Bf[count];


    std::mt19937 re(rd());
    std::uniform_real_distribution<d_type::Bfloat> ui(0, 1);

    for(d_type::Bsize i=0; i<count; i++)
    {
        samples[i]=Vector2Bf(ui(re),ui(re));
    }


    return samples;
}

And Its works just as it should.




Aucun commentaire:

Enregistrer un commentaire