From: "Daniel H. Luecking" Newsgroups: comp.os.msdos.djgpp Subject: Re: randN Date: Tue, 23 Nov 1999 11:45:54 -0600 Organization: The University of Arkansas Lines: 42 Message-ID: References: <383904BA DOT AE45DCDE AT mpx DOT com DOT au> <81bigt$1i5 AT acp3bf DOT knirsch DOT de> NNTP-Posting-Host: comp.uark.edu Mime-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII In-Reply-To: To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com X-Mailing-List: djgpp AT delorie DOT com X-Unsubscribes-To: listserv AT delorie DOT com Precedence: bulk On Tue, 23 Nov 1999, Eli Zaretskii wrote: > > On 22 Nov 1999, Hans-Bernhard Broeker wrote: > > > The typical method works by transforming the distribution. > > A much simpler way is to generate 6 uniform random number (by calling > e.g. rand() 6 times), then return their average, suitably scaled to > give the normal Gaussian deviate (if I'm not mistaken, subtract 3 from > the average and divide the result by the square root of 2). You are (slightly) mistaken. > While seemingly stupid, this method is actually good enough to be > included as part of respectable numerical libraries such as IMSL. The > theory behind it is the well-known theorem which states that the sum > of a large number of independent random numbers approaches the normal > distribution. In this theorem, one needs mean of 0 and variance 1, and then use the sum of N random numbers divided by sqrt(N). To achieve mean 0, subtract RAND_MAX/2 fron the numbers returned by rand(). To make them have variance 1, divide by RAND_MAX/(2*sqrt(3)) Now sum them and divide by sqrt(N). If N=6 and you sum the numbers first, then divide the sum by RAND_MAX, subtract 3 and _multiply_ by sqrt(2). A disadvantage is that one will never get a result greater than 3*sqrt(2) in absolute value and the distribution is pretty far off near these extremes. Applications may go wildly wrong if one needs to allow large deviations or one takes very large samples. Of course the theorem can be applied with arbitrary N much greater than 6 to get better results. -- Dan Luecking Dept. of Mathematical Sciences luecking AT comp DOT uark DOT edu University of Arkansas http://comp.uark.edu/~luecking/ Fayetteville, AR 72101