samedi 16 avril 2016

How to seed random number generator?

I was looking at the example for generating normal distributed random numbers at cppreference.com and refactored the code a bit to get this:

#include <iostream>
#include <random>

struct MyNormalDistribution {
    static double getRandomNumber(double mean,double std_dev){
        return std::normal_distribution<>(mean,std_dev)(MyNormalDistribution::generator);
    }
    private:
        static std::random_device rand;
        static std::mt19937 generator;
};
std::random_device MyNormalDistribution::rand;
std::mt19937 MyNormalDistribution::generator = std::mt19937(MyNormalDistribution::rand());

int main(int argc, char *argv[]) {
    for (int t=0;t<10;t++){
        std::cout << MyNormalDistribution::getRandomNumber(0,10) << std::endl;
    }
}

However, whenever I run this I get the same sequence of numbers. Is there some stupid mistake, or does the example on cppreference not include proper seeding?

How to I properly seed MyNormalDistribution ?




Aucun commentaire:

Enregistrer un commentaire