float BoxMuller(float mean, float sd)
{
float x1, x2, w, y1; // polar coordinates
static float y2;
static int use_last = 0;
if (use_last)
{
y1 = y2;
use_last = 0;
}
else
{
do
{
x1 = 2.0 * random(1.0) - 1.0;
x2 = 2.0 * random(1.0) - 1.0;
w = x1 * x1 + x2 * x2;
}
while ( w >= 1.0 );
w = sqrt( (-2.0 * log( w ) ) / w );
y1 = x1 * w;
y2 = x2 * w;
use_last = 1;
}
return( mean + y1 * sd );
}
function main()
{
random_seed(0); // really random seed
float random_value = BoxMuller(5,2); // get random value from a Normal Distribution with a mean of 5 and standard deviation of 2.
}