Wanted: RNG with bell-curve distro

Quote:

>I'm looking for a random number generator which generates random numbers with a

>bell curve distribution.

tFloat is whatever floating point type you want. Give it the mean and

standard deviation you want. rNormal returns the result. rNormal2

returns two of them in less time than 2 calls to rNormal.

function rNormal( const mean, sd : tFloat) : tFloat;

{ Returns a FP number that is normal with the given mean }

{ and standard deviation. Based on the method of G. E. P. }

{ Box, M. E. Muller, and G. Marsaglia in D. E. Knuth, The }

{ Art of Computer Programming, Volume 2, 3rd ed., pg. 122 }

{ By Jud McCranie, Dec 2, 1986. Slightly revised 5/30/99, 7/14/00 }

{ if execution time is a consideration, it can be modified

to return two variables with each call. See rNormal2 }

var u1, u2, v1, v2, s : tFloat;

begin { --- rNormal --- }

repeat

u1 := random;

u2 := random;

v1 := 2.0 * u1 - 1.0;

v2 := 2.0 * u2 - 1.0;

s := sqr( v1) + sqr( v2);

until s < 1.0;

rNormal := mean + sd * sqrt( (-2.0 * ln( s) / s)) * v2;

end; { --- r normal --- }

procedure rNormal2( const mean, sd : tFloat;

out x1, x2 : tFloat);

{ Returns reals X1 and X2 that are normally distributed with }

{ the given mean and standard deviation. Based on the method }

{ of G. E. P. Box, M. E. Muller, and G. Marsaglia in D. E. Knuth, }

{ The Art of Computer Programming, Volume 2, 3rd ed., pg. 122 }

{ By Jud McCranie, Dec 2, 1986. Slightly revised 5/31/99, 7/14/00 }

var u1, u2, v1, v2, s, thing : tFloat;

begin { --- rNormal2 --- }

repeat

u1 := random;

u2 := random;

v1 := 2.0 * u1 - 1.0;

v2 := 2.0 * u2 - 1.0;

s := sqr( v1) + sqr( v2);

until s < 1.0;

thing := sqrt( (-2.0 * ln( s)) / s);

x1 := mean + sd * v1 * thing;

x2 := mean + sd * v2 * thing;

end; { --- r normal2 --- }