Mingw随机数发生器发生器

时间:2015-10-22 11:53:16

标签: c++ c++11 random mingw

我使用随机数生成器在MinGW 4.8.1-4上遇到问题。

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

此函数始终返回相同的数字。

在使用gcc的Linux上,问题没有发生......所以...我该怎么做才能在Windows上用c ++生成数字?

修改

这一切都很好......我是如此愚蠢,以至于我没有读到std::random_devicestd::mt19937 gen的规格。 所以我做了类似的事情:

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

它的工作原理应该如此。

0 个答案:

没有答案