Originally Posted by

**sh3rpa**
Code:

double normal(const double &mean, const double &sdiv)
{
static const double r_max = RAND_MAX +1;
return sdiv * sqrt( -2 * log( (rand() + 1) / r_max ) ) * sin ( 2 * PI * rand() / r_max) + mean;
}

this is the standard deviation function i've used in my AI classes. Now, this returns a *random* number that is within the deviation that you set (as const double &sdiv)

But it might help, shoot, I dunno.

It's funny – I didn't know there was such an easy method to get a normal distributed number, by just inserting two "ordinary" random numbers into a short formula. Since I saw your post I've been bussy trying to find the probably distribution of the numbers generated, eventually I found an article at wikipedia.

This method is called Box-Muller transform. What it does is that it generates a normal distributed complex number in polar form, then it extracts the imaginary part of it. But since the real and the imaginary part – of the kind of normal distributed complex numbers this method is generating – is independent, you can actually get *two* perfectly normal distributed and independent real numbers, the other is the real part of the number from it, and you get it by taking cosinus of the angle instead of sinus.

So, you could have a function which takes two random numbers between 0 and 1 and transforms them into normal distributed numbers:

Code:

int normal(const double &mean, const double &sdiv,
double &r1, double &r2) { //r1 and r2 contains the real return values, the int is just for error handling
if (r1 <= 0 or r1 > 1) return 1;
double abs = sdiv * sqrt(-2*log(r1));
r1 = mean + abs*sin(2*PI*r2);
r2 = mean + abs*cos(2*PI*r2);
return 0;
}