Normal distribution random number generator -- WANTED

Wilf Leblanc wilf at sce.carleton.ca
Tue Dec 4 16:06:48 AEST 1990


adkins at iguana.cis.ohio-state.edu (Brian Adkins) writes:

>>I am looking for C source code to generate normal distribution
>>random numbers. I want to be able to generate random numbers
>>in the range of 0 - N (N could be equal to 9999, 99999 or 999999) that 
>>have normal distribution with mean, say, u and standard deviation s.

>I'm not sure if this is exactly what you want, but the Box-Muller method
>takes a pair of independent uniform variables ("regular random numbers") 
>and creates a pair of standard normal variables:

>   z1 = sqrt(-ln(u2)) * cos(2*PI*u1)
>   z2 = sqrt(-ln(u2)) * sin(2*PI*u1)

>where the angle is expressed in radians.  You probably can convert from 
>standard normal variables to the specific case, but for completeness:

>   x1 = u + s * z1,  x2 = u + s * z2   where u is mean, s is std. dev.

>this method is almost universally preferred over solving for r = F(z)

>most of this info is from "Probability & Statistics for Engineers" Miller...

Assume for a moment the mean (u) is zero and s is the standard deviation.
If N/s > 10 then the problem amounts to getting a good
normal random number generator which accurately models the tails of
the normal distribution.  i.e. if we are using 32 bit integers for
the uniform random number generator and u2 is non-zero (it must be)
then the maximum value of z1 or z2 is sqrt(-ln(min(u2))). If min(u2) is
1/2^31, then the maximum is sqrt(ln(2^31)) = 4.6.  (If min(u2) is 2^63,
the max is 6.6).

I guess the whole point is to use many bits in the random number generator
if you want to model the tails accurately.  In some applications, the tails
are very important.  One example is very, very low error rate digital
communications.  If you are not careful, you can easily get funny behavoir
in the tail of the distribution.

Maybe this wasn't really the original problem, but this is what I thought.

Is there a better way (rather than just adding many, many, uniform random
numbers) ?  I guess one could use the above formula, and add succesive
outputs (and scale of course).  If we were to add 16 consecutive normals,
(using the above algorithm) then divide by 4, we would obtain a more accurate
tail.

I usually use the above method, but my applications rarely require a very
accurate tail.
--
Wilf LeBlanc                          Carleton University
Internet: wilf at sce.carleton.ca        Systems & Computer Eng.
    UUCP: ...!uunet!mitel!sce!wilf    Ottawa, Ont, Canada, K1S 5B6



More information about the Comp.lang.c mailing list