Re: [LONG] Random number generator (normal random source)

From: Carlo Mocci (mocci@TIN.IT)
Date: 07/10/98


At 17.08 10/07/98 -0400, you wrote:
>"d. hall" <dhall@OOI.NET> writes:
>
>> // thus on Fri, 10 Jul 1998 13:15:55 -0400, James wrote:
>>
>> > In my code I needed numbers generated on a normal distribution (ie the
>> > bell curve thing) and use floats for that purpose.  Anyway, here's the
>> > code.
>>
>> Normal (aka Gaussian) random number generation reminds me...  herein is a
>> function is heavily bastardized from Numerical Recipes in C.
>
>This is the same code I used.  I took the algorithm from Knuth
>though.  I didn't mean to imply I needed such code; I've had it for a
>while and have had interesting success with it :)

Beside from this "exact" method of generating gaussian variables, you could
always use the Central Limit Thereme (? hope this is correct in English) by
generating N uniform variables between 0 and 1.
Usually N=12 is good.
The sum of this N variables is distributed (for N -> inf) as a Normal.

Sum of N U(0,1)-> will be distributed as a Gaussian of average N*0.5 and
variance will be N times the variance of the uniform distribution (can't
remember right now, but it seems to me it is 1/12).

So basically, ((SUM of N U(0,1)) - 6 )/sqrt(N*Variance(U(0,1)))
approximates to a Standard Gaussian N(0,1).

If Variance(U(0,1)) = 1/12 -> You will have that for N=12 the deviation
part of the formula is 1.

Only problem with this, is that you will never get random variables from
the very left or right part of the normal queue, but for normal application
this should be good (think of a height/weight distribution -> nobody would
have a negative height ! with a "correct" gaussian distribution, he could
have).

This method should be fast too, because it only needs to sum N (12) random
variables, instead of doing log (and if I remember well... the log function
is already defined in Circle ;-)   ). An exact comparison of speed should
be made on the speed of the RNG anyway.

Sorry for the long (and full of English errors) mail.


Carlo Mocci - mocci@tin.it

Kabir
Coder of Dalila MUD
dalila.shopitaly.net 4000


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     | http://democracy.queensu.ca/~fletcher/Circle/list-faq.html |
     +------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/15/00 PST