Subject: Re: From the Trenches There Has To be A Better Way #23a
From: rpw3@rpw3.org (Rob Warnock)
Date: Fri, 27 Feb 2009 21:41:08 -0600
Newsgroups: comp.lang.lisp
Message-ID: <-P2dnRUVMMHJLzXUnZ2dnUVZ_uSdnZ2d@speakeasy.net>
Kenneth Tilton  <kentilton@gmail.com> wrote:
+---------------
| But reviewing:
| (defun quick-n-dirty-normal (&optional (mean 0d0) (dev 1d0))
|    (+ mean (* dev (- (loop :repeat 12 :summing (random 1d0)) 6))))
| 
| We see the -6<->6 value multiplied by the s.dev to get an actual 
| deflection. If I had looped 50 times and subtracted 25 I would have a 
| range of -25<->25 and still be multiplying by the s.dev.
| 
| I eyeballed twenty values from qnd and they looked OK (I did 100 w sdev 
| 15 since that was offered under the name IQ) and I pretty much 
| recognized the crowd at my sports bar so I thought 6 was magic.
+---------------

Since no-one else has mentioned it yet, I feel obligated to present
a reminder that nothing that truncates the range of the distribution
(such as the above Q&D summation) can actually be a "normal" or "Gaussian"
distribution, since the tails of latter extend to infinity on both sides.
Always. [Albeit with however low a probability...]  But since you're
only looking for a QUICK-N-DIRTY-NORMAL, that's probably not an issue
for you. But it just needed saying, 'kay?

On the other hand, Scott's 3-liner:

    (defun random-IQ-score ()
      (+ 100 (* 15 (sqrt (* -2 (log (random 1.0))))
		   (cos (* 2 pi (random 1.0))))))

*does* have unlimited tails [good], but can [rarely, *very* rarely!]
blow up with an arithmetic exception in the case when (RANDOM 1.0)
returns exactly 0.0 [oops].

This should be safe from blowups [though still not from negative IQs],
though it biases the results a tiny bit [a really, *really* tiny bit!]:

    (defun random-IQ-score ()
      (+ 100 (* 15 (sqrt (* -2 (log (+ (random 1d0)
				       least-positive-double-float))))
		   (cos (* 2 pi (random 1d0))))))

With IEEE doubles, the range of this is now approximately -479 to +679.
But in 10 million trials, fewer than a dozen times was the result less
than 25 or more than 175...


-Rob

-----
Rob Warnock			<rpw3@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607