lvaruzza / cl-randist

Random number generation for common lisp
http://code.google.com/p/cl-randist/
20 stars 10 forks source link

Reproducible results on other lisps will be un-reproducible on CMUCL and SBCL #5

Closed rpgoldman closed 10 years ago

rpgoldman commented 10 years ago

Because of this:

   #+(or sbcl cmucl)
   (progn
     (declaim (inline random-mt))
     (defun random-mt (x &optional (state *random-state*))
       (random x state)))

The following, which will work on other lisps will fail on CMUCL and SBCL

 (let* ((state (make-mt-random-state))
         (state1  (make-mt-random-state state))
         (state2  (make-mt-random-state state))
         foo bar)
    (let ((*mt-random-state* state1))
      (setf foo
        (iter (for i from 0 below 10000)
               (collecting (random-uniform)))))
    (let ((*mt-random-state* state2))
      (setf bar
        (iter (for i from 0 below 10000)
               (collecting (random-uniform)))))
   (equalp foo bar))

For other lisps, all we need to do is control *MT-RANDOM-STATE*, but this optimization changes the semantics of RANDOM-UNIFORM so that it requires trapping *RANDOM-STATE*.

The cost to correctness of this optimization seems higher than the benefit to efficiency. I suggest you kill it.