Wanted: Gaussian Random Number Generator

Ken Johnson ken at aiai.ed.ac.uk
Sun Sep 24 06:42:33 AEST 1989


>	   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 at 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.'



More information about the Comp.lang.c mailing list