# Board index » delphi » Wanted: RNG with bell-curve distro

## Wanted: RNG with bell-curve distro

I'm looking for a random number generator which generates random numbers with a
bell curve distribution. For example, if I gave the RNG a range of 0-100, the
most numbers generated near 50. If I gave a range of -4 to +4, then most
numbers would be generated near 0.

Pascal preferred, but other languages ok.
Thanks.

--
Freezone Freeware and Free Delphi Components
http://freezone.darksoft.co.nz http://chuckr.bravepages.com
http://www.bdsg.com/resources

## Re:Wanted: RNG with bell-curve distro

Hi Chuck!

##### Quote
Chuck S. wrote:
> I'm looking for a random number generator which generates random
> numbers with a bell curve distribution. For example, if I gave the
> RNG a range of 0-100, the most numbers generated near 50. If I gave a
> range of -4 to +4, then most numbers would be generated near 0.

just a quick idea:

function myrandom(max: integer) : integer;
begin
myrandom := trunc((random + random) * (max+1) / 2.0 )
{ not 2 * random :-)) }
end;

for range 0..100 use myrandom(100);
to achieve a range of -4 to 4 for example use 'r := myrandom(8) - 4;'

i'm really not sure, if this function will result in the distribution
you're looking for.

kind regards
jochen

## Re:Wanted: RNG with bell-curve distro

##### Quote
> I'm looking for a random number generator which generates random numbers
with a
> bell curve distribution. For example, if I gave the RNG a range of 0-100,
the
> most numbers generated near 50. If I gave a range of -4 to +4, then most
> numbers would be generated near 0.

Maybe you mean a Gauss-function? In that case, you should download
a bunch of SWAG-files from (for example) ftp://garbo.uwasa.fi/pc/turbopas
and search for 'gauss'.

Hope that will help

## Re:Wanted: RNG with bell-curve distro

JRS:  In article <bebt88\$3blb...@ID-185685.news.dfncis.de>, seen in
news:comp.lang.pascal.misc, Chuck S. <ch...@gil.net> posted at Mon, 7
Jul 2003 13:40:25 :-

##### Quote
>I'm looking for a random number generator which generates random numbers with a
>bell curve distribution. For example, if I gave the RNG a range of 0-100, the
>most numbers generated near 50. If I gave a range of -4 to +4, then most
>numbers would be generated near 0.

<URL:http://www.merlyn.demon.co.uk/pas-rand.htm#RandDist>

--
? John Stockton, Surrey, UK.  ?...@merlyn.demon.co.uk   Turnpike v4.00   MIME. ?
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
<URL:http://www.merlyn.demon.co.uk/clpb-faq.txt>   RAH Prins : c.l.p.b mFAQ;
<URL:ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ.

## Re:Wanted: RNG with bell-curve distro

On Mon, 7 Jul 2003 20:43:05 +0100, Dr John Stockton

##### Quote
<s...@merlyn.demon.co.uk> wrote:
>JRS:  In article <bebt88\$3blb...@ID-185685.news.dfncis.de>, seen in
>news:comp.lang.pascal.misc, Chuck S. <ch...@gil.net> posted at Mon, 7
>Jul 2003 13:40:25 :-
>>I'm looking for a random number generator which generates random numbers with a
>>bell curve distribution. For example, if I gave the RNG a range of 0-100, the
>>most numbers generated near 50. If I gave a range of -4 to +4, then most
>>numbers would be generated near 0.

> <URL:http://www.merlyn.demon.co.uk/pas-rand.htm#RandDist>

Just for fun (and to learn something), I read John Stockton's web
page, above, and saw my version of Gauss discussed.  Of course,
there's also the simple-minded approximation from the "Mean tends to a
Gaussian" -- simply add up 12 uniformly-distributed random numbers,
which will be approximately a Gaussian with a mean of 6 and a standard
deviation of [here I forget the details, but it is fairly simple to
derive].  I learned this one years ago from the IBM Scientific
Subroutine Package, written in Fortran II (not IV!).

Bob Schor
Pascal Enthusiast

## Re:Wanted: RNG with bell-curve distro

##### Quote
Chuck S. wrote:
> I'm looking for a random number generator which generates random numbers with a
> bell curve distribution. For example, if I gave the RNG a range of 0-100, the
> most numbers generated near 50. If I gave a range of -4 to +4, then most
> numbers would be generated near 0.

> Pascal preferred, but other languages ok.
> Thanks.

function ZufallNormal (Mittel, SigmaSqr : double) : double;

begin
ZufallNormal := sqrt(-2 * ln(random)) * sin(2 * pi * random) *
SigmaSqr + Mittel;
end;

## Re:Wanted: RNG with bell-curve distro

On 7 Jul 2003 13:40:25 GMT, "Chuck S." <ch...@gil.net> wrote:

##### 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 --- }