double randNormal(double (*fun)(double, double, double), double xmin, double xmax, double sigma, double mju)
{
static double (*Fun)(double, double, double) = NULL, YMin, YMax;
static bool First = true;
if (First)
{
First = false;
srand((unsigned) time(NULL));
}
if (fun != Fun)
{
Fun = fun;
YMin = 0, YMax = Fun(xmin, sigma, mju);
for (int iX = 1; iX < 10000; iX++)
{
double X = xmin + (xmax - xmin) * iX / 10000;
double Y = Fun(X, sigma, mju);
YMax = Y > YMax ? Y : YMax;
}
}
double X = xmin + (xmax - xmin) * rand() / RAND_MAX;
double Y = YMin + (YMax - YMin) * rand() / RAND_MAX;
return Y < fun(X, sigma, mju) ? X : randomNormal(Fun, xmin, xmax, sigma, mju);
}
I am very new to C++ and I am struggling with understanding the code above. What is the role of (*fun)(double, double, double)
when we define the function randNormal
? Furthermore, what is accomplished by the second line starting with static double
? I would appreciate your help!