Hi Disch, your solution **is not** good enough: you cannot replace the integral of a function with the function itself and expect any good results. Please don't use your work as is!
You need to tell us a few important things:
1. When you generate your number, do you generate a double with the specified parameters, and then round it off?
2. How do you round off - nearest integer?
3. The function capital Phi (cumulative distribution function - see wikipedia link from code comments) measures the chance of getting a **double** equal to or smaller than val (using normalized bell curve).
4. If you are aiming to get the number 10, and you are rounding off to nearest integer, then what you need is
Phi(10.5)-Phi(9.5). Phi(10.5)-Phi(9.5) measures the chance of getting a number less than 10.5 (all numbers that will get rounded to 10 or less) minus chance of getting a number less than 9.5 (all numbers that will get rounded to 9 or less). However you have to tell us exactly how you generate your number (if you floor it, or if you round it, the computation will change essentially).
5. The above considerations were for normalized distribution. So if you use round of a double, what you will need in the end is
1 2 3 4
|
int goal=10;
double whatYouNeedIfUsingRound=
getPercentageNormalDistribution( ((double) goal)+0.5, yourMean, yourDeviation)-
getPercentageNormalDistribution( ((double) goal)-0.5, yourMean, yourDeviation);
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
namespace c
{
const double e = 2.71828182845904523536;
const double pi = 3.14159265358979323846;
const double rad2pi = std::sqrt(2*pi);
const int numIterations =100;
}
double getPercentageNormalDistribution(double val, double mean, double stddev) //all arguments should be floating point, int is no good!
{ return getNormalDistributionPhi((val-mean)/stddev);
}
double getNormalDistributionPhi(double x)
{ //reference: http://en.wikipedia.org/wiki/Normal_distribution#Cumulative_distribution_function
//code copied from pascal from Wikipedia and translated to c++. I have not made any checks for errors.
//Please give the code a second look.
//Original pascal code from wikipedia:
//begin
// sum:=x;
// value:=x;
// for i:=1 to 100 do
// begin
// value:=(value*x*x/(2*i+1));
// sum:=sum+value;
// end;
// result:=0.5+(sum/sqrt(2*pi))*exp(-(x*x)/2);
//end;
double value=x;
double sum=x;
for (int i=1; i<c::numIterations; i++)
{ value*=x*x/(2*i+1);
sum+=value;
}
return 0.5+(sum/std::sqrt(2*pi))*std::exp(-(x*x)/2);
}
|