lundi 20 juillet 2020

How to avoid same sequences of random numbers

I would like to generate different sequences of uniformly distributed samples. To this end, I initialize the default random engine with different seeds, but the same sequences are produced:

#include <iostream>
#include <random>

void fun(double seed)
{
    std::cout << "given seed: " << seed << std::endl;
    std::default_random_engine gen_2(seed);
    std::uniform_real_distribution<double> dis_2(0.0,1.0);
    std::cout << dis_2(gen_2) << std::endl;
    std::cout << dis_2(gen_2) << std::endl;
}

int main()
{
    double seed = 1.0;
    std::default_random_engine gen_1(seed);
    std::uniform_real_distribution<double> dis_1(0.0,1.0);
    for(size_t i=0; i<3; ++i)
    {
        fun(dis_1(gen_1));
    }
}

The output reads:

given seed: 0.0850324

0.0850324

0.891611

given seed: 0.891611

0.0850324

0.891611

given seed: 0.18969

0.0850324

0.891611

How can I produce different sequences in the function fun?




Aucun commentaire:

Enregistrer un commentaire