Xref: utzoo sci.math:5671 comp.sources.wanted:6347 Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!bloom-beacon!apple!well!pokey From: pokey@well.UUCP (Jef Poskanzer) Newsgroups: sci.math,comp.sources.wanted Subject: Re: random numbers Summary: right for the wrong reason Message-ID: <10708@well.UUCP> Date: 14 Feb 89 09:43:56 GMT References: <734@fcs280s.ncifcrf.gov> Reply-To: Jef Poskanzer Distribution: na Organization: Paratheo-Anametamystikhood Of Eris Esoteric, Ada Lovelace Cabal Lines: 45 In the referenced message, toms@ncifcrf.gov (Tom Schneider) wrote: }In article <10471@ut-emx.UUCP> ken@ut-emx.UUCP (kenneth Moore) writes: }>I am looking for a program to generate -Normal distributed }> random numbers } }The simplest way to generate such numbers is to add together several }flat distribution numbers. This is very costly, and there is a better way. } theta = Un 2 pi } r = sqrt(-2 ln(Un+1)) }then when these polar coordinates are converted to Cartesian }coordinates, one gets two independent Normally distributed numbers Excuse me, but for what meaning of the word "costly" is generating several uniform numbers even SLIGHTLY more costly than generating two uniform numbers and then doing a log, a square root, and a sine or cosine?? Not to mention all those multiplies. I prefer the polar method too (see below for an implementation in C, straight out of Knuth), but not because of "cost". I use it because it's *correct*. The "add up N uniforms" method is only an approximation. --- Jef Jef Poskanzer jef@helios.ee.lbl.gov ...well!pokey "No wonder white people are going to Hell..." -- Andrew Purshottam float rndnor( mean, stddev ) float mean, stddev; { float v1, v2, z; do { v1 = -log( 1.0 - rnd() ); v2 = -log( 1.0 - rnd() ); } while ( 2.0 * v1 < ( (v2-1.0)*(v2-1.0) ) ); if ( rnd() > 0.5 ) z = 1.0; else z = -1.0; return stddev * z * v2 + mean; }