Randoms and the edges of the segment

Pondering the philosophy behind the language
Locked
Kazimir Majorinc
Posts: 388
Joined: Thu May 08, 2008 1:24 am
Location: Croatia
Contact:

Randoms and the edges of the segment

Post by Kazimir Majorinc »

I noticed following behaviour of the random primitive.

Code: Select all

> (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.

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Re: Randoms and the edges of the segment

Post by Lutz »

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.

Locked