Moataz wrote:
Dear All,
Can anyone tell me where to find source code or even the
algorithm for simulating The normal distribution and the Exponential
distribution ?
Exponential distribution is easy: the negative logs of uniform random
numbers are exponentially distributed. Do trap the case X=0 before
transforming, as your RNG will probably yield this with P0.
Normal is done using a clever trick closely related to the one used to
integrate the area under the normal curve; the idea is that if you take
the joint distribution of *two* independent normal variables in polar
coordinates, the extra r in the polar area element (r dr dtheta) is
precisely what you need to integrate
e^-x^2 e^-y^2 = e^-(x^2 + y^2) = e^-r^2
by substitution. The function you get after the substitution is an
exponential (which we know how to do); it follows that if U,V are
uniform on [0,1],
sqrt(-2 ln U) cos (2 pi V)
is normally distributed. Moreover,
sqrt(-2 ln U) sin (2 pi V)
is independently normally distributed; so with careful coding you get
two independent normal RV's out of this calculation for little more than
the price of one. Again, the careful programmer will trap the case
U=0.0 (note that this is one of the VERY few times when an exact test
for equality between floats is valid; alternatively you can do the
calculation within a try{} block or the equivalent) . *No* other
nonnegative value, no matter how tiny, should cause a floating-point
error on any normal system, as the ln of the least positive number on
most systems will be greater than (say) -10,000.
Finally, you ought to be sure your original uniform PRNG is good. Many
of those in standard libraries are not, having (for instance) sequential
dependence.
Moreover, if you create U[0,1] PRV's from short integer RV's (say
0...65535) you may have noticeable granularity in the far tails.
George Marsaglia has produced several excellent PRNG's, easy to code and
available in the literature.
Happy New Year
Robert Dawson
PS: To the regular posters: If EDSTAT-L were to have a FAQ list, this
question would be a good candidate.
=
Instructions for joining and leaving this list, remarks about the
problem of INAPPROPRIATE MESSAGES, and archives are available at
http://jse.stat.ncsu.edu/
=