I've managed to get a working normal distribution in my main() function using the following code:
default_random_engine generator(time(0));
normal_distribution<double> distribution(6.0,5.0);
for (int i = 0; i < 51; i++)
{
cout << "#" << i << "= " << distribution(generator);
}
This will happily print out 50 values that follow a distribution with a mean of 6.0 and an average of 5.0 for example:
#0= 0.999998
#1= 3.43872
#2= 6.83918
#3= -3.81324
#4= 16.0962
#5= 1.26236
However, as soon as i move the same code into a method and call said method to return individual values that will populate the output, it only returns the same value over again:
double normalGenerator(double mean, double stdDev)
{
default_random_engine generator(time(0));
normal_distribution<double> distribution(mean,stdDev);
return distribution(generator);
}
(in main()):
for (int i = 0; i < 51; i++)
{
cout << "#" << i << "= " << distribution(generator);
cout << " " << normalGenerator(6.0,5.0) << endl;
}
Output:
#0= 5.49136 5.49136
#1= 7.6902 5.49136
#2= 14.4971 5.49136
#3= 11.946 5.49136
#4= 4.37424 5.49136
#5= 9.91711 5.49136
The first column is the original code, the second is the method-called value.
I assume my issue is based on the
default_random_engine generator(time(0));
normal_distribution<double> distribution(6.0,5.0);
lines being in the method, but my understanding of how they work isn't great and i haven't found any good beginner-level documentation to help me understand it.
Aucun commentaire:
Enregistrer un commentaire