vendredi 12 janvier 2018

C++ generates same random result

#include <iostream>
#include <random>
using namespace std;
float rnorm(float mean, float sigma);
int main()
{
    int i;
    float sample;
    for(i = 0; i < 10; ++i)
    {
        cout<<rnorm(0,1)<< endl;
    }
}

float rnorm(float mean, float sigma) {
    std::default_random_engine generator( std::random_device{}() );
    std::random_device rd;   
    std::mt19937 gen(rd()); 
    float result;
    std::normal_distribution<float> d(0, 1); 
    result = d(gen); 
    return result; 
}

I wrote the code above to generate 10 random normal values. But the problem is that all values are the same and they don't change every time I run the program. I read on this site that I needed to add the line std::default_random_engine generator( std::random_device{}() );. However, nothing changed after I add the line to the rnorm function.




Aucun commentaire:

Enregistrer un commentaire