Path: utzoo!attcan!uunet!mcsun!ukc!edcastle!aiai!ken
From: ken@aiai.ed.ac.uk (Ken Johnson)
Newsgroups: comp.lang.c
Subject: Re: Wanted: Gaussian Random Number Generator
Message-ID: <927@skye.ed.ac.uk>
Date: 23 Sep 89 20:42:33 GMT
References: <1820002@hpsad.HP.COM>
Reply-To: ken@aiai.UUCP (Ken Johnson)
Organization: AIAI, University of Edinburgh, Scotland
Lines: 36

>	   I'm looking for a random number routine which will generate
>	numbers with a Gaussian distribution. Rand(3C) and other such
>	programs produce (semi) even distributions.

Assuming that rand( ) returns a rectangular distribution: [this is
untested and based on what I remember of the old SPSS library]

gauss(mean,sd)	/* sd: standard deviation */
float mean;
float sd;
{
	float t;
	float rand( );
	int i;

	t = 0.0;

	for (i = 0; i < 12; ++i)
	{
		t += rand( );
	}

	return(sd * ((t - 6.0) / 12.0));
}


The 12 is a magic number, so don't change it.  Summing 12 rectangular
distributions gives a gaussian one.  If anyone can explain why clearly
enough for me to understand, I will send them a Beer Token. 


-- 
Ken Johnson, AI Applications Institute, 80 South Bridge, Edinburgh EH1 1HN
E-mail ken@aiai.ed.ac.uk, phone 031-225 4464 extension 212
`I have read your article, Mr.  Johnson, and I am no wiser than when I
started.' -- `Possibly not, sir, but far better informed.'