Randoms and the edges of the segment

Started by Kazimir Majorinc, June 29, 2010, 12:56:27 PM

Previous topic - Next topic

Kazimir Majorinc

I noticed following behaviour of the random primitive.


> (dotimes(i 200000)(let((r (random)))(unless (and (< 0 r) (< r 1))(println "edge " (inc counter) ". " r))))
edge 1. 1
edge 2. 1
edge 3. 0
edge 4. 1
edge 5. 1
edge 6. 1
edge 7. 0
edge 8. 1
edge 9. 0
edge 10. 1
edge 11. 0
true


The first surprise is that edge cases happen - but OK, it is matter of design; I don't know is it mentioned in manual. Other surprise is that edge cases happen roughly once in 16384 times and that might be too frequent - if random returns some ten significant digits.
http://kazimirmajorinc.com/\">WWW site; http://kazimirmajorinc.blogspot.com\">blog.

Lutz

#1
This only happens on Windows where the raw internal random generator only returns integer values between 0 and 32767 (15 bits).  On most Unixes you will (almost) never see it, where the internal random generators return 31 significant bits.