From: Jeffrey Cunningham <jeffrey@jk...>  20120708 03:29:19

On 07/07/2012 07:23 PM, Burton Samograd wrote: > On 120707 07:03 PM, Kevin Reid wrote: >> On Jul 7, 2012, at 17:44, Burton Samograd wrote: >> >>> Here is the definition of normal, which has no divisions: >>> >>> (defun normal (&optional (a 1.0) (s 0.5)) >>> (+ a (* (sqrt (* 2 (log (random 1.0)))) (cos (* 2 pi (random 1.0))) s))) >>> >>> Any idea where this division by zero error might be coming from? >> RANDOM may return 0.0 in which case LOG will (on SBCL) signal DIVISIONBYZERO. >> > Ah, I had a feeling it was something like that but the division by zero > was throwing me off. > > Thanks. I got that line from Rosetta Code; maybe it should be fixed. > What is a good fix for this? I'm thinking that adding a very small value > to the result of random might suffice but I don't like it. > >  > Burton Samograd Adding a small amount would warp the distribution (a tiny amount). But this will work and won't: (defun normal (&optional (a 1.0) (s 0.5)) (+ a (* (sqrt (* 2 (log ( 1.0 (random 1.0))))) (cos (* 2 pi (random 1.0))) s))) According to the spec, random produces numbers always less than the limit so it will never hit zero. And its a uniform distribution so you haven't changed its statistical properties. Jeff Cunningham 