Subject: Re: From the Trenches There Has To be A Better Way #23a
From: rpw3@rpw3.org (Rob Warnock)
Date: Tue, 03 Mar 2009 03:16:41 -0600
Newsgroups: comp.lang.lisp
Message-ID: <6padnTADmb3kaDHUnZ2dnUVZ_gKWnZ2d@speakeasy.net>
Thomas A. Russ <tar@sevak.isi.edu> wrote:
+---------------
| rpw3@rpw3.org (Rob Warnock) writes:
| > 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))))))
| 
| Does it really bias it any more than before?
| The reason is because the range of (random 1.0d0) is actually [0.0, 1.0)
| and not [0.0, 1.0], and this is presumably being shifted slightly to
| to (0.0, 1.0), since the least-positive-double-float won't be large
| enough to change the value of 1.0d0 when added.  ...
+---------------

Actually, as Tamas Papp pointed out, the correct domain for the
Box-Muller algorithm is (0.0, 1.0] anyway, so that "the right thing"
[after Scott corrected Tamas's typo] is to use (- 1d0 (random 1d0))
instead, and so now we're back to three lines:

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


-Rob

p.s. Scott also suggested using the SIN result rather than COS,
but you're going to lose one point from the range in any case.
I don't see that it matters from which end.

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