# Is it really possible for rand to throw a zero?

**URL:** <https://discourse.julialang.org/t/is-it-really-possible-for-rand-to-throw-a-zero/28505>\
**Category:** Statistics\
**Tags:** question\
**Created:** [September 7, 2019, 4:29pm UTC](https://discourse.julialang.org/t/is-it-really-possible-for-rand-to-throw-a-zero/28505 "2019-09-07T16:29:09Z")\
**Posts on this page:** 1\
**Showing post:** 44

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [September 11, 2019, 9:21am UTC](https://discourse.julialang.org/t/is-it-really-possible-for-rand-to-throw-a-zero/28505/44 "2019-09-11T09:21:58Z")

</div>

The more I’ve thought about this, the more I’ve come to believe that the current Julia (and C, C++, Fortran, Python, Perl and Ruby) implementations are wrong. Here’s why:

The `rand` function does not actually draw from the infinite set of points on the (open or closed) interval. It draws from the finite set `0:eps(T):1-eps(T)`. (Which has the convenient property that the elements can be represented in type `T`.)

We might want to think about `rand` as drawing from the infinite set (the open or closed interval) and then projecting (rounding) onto a finite set of floating-point numbers. But for that metaphor to work with the above-mentioned set, the interval must be `(-eps(T)/2, 1-eps(T)/2)`. (The open or the closed interval. Mathematically, there’s no difference. The probability of hitting an endpoint in a finite number of draws is zero.)

Setting the least significant bit to 1, as suggested by @rfourquet would be equiivalent to drawing from `eps(T)/2:eps(T):1-eps(T)/2`, which in turn would be equivalent to drawing from the (open or closed) continuous interval (0,1) and then correctly rounding to the nearest member of that set. This would not cost any extra time, and is the correct fix, i.m.o.

---

_[View the full topic](https://discourse.julialang.org/t/is-it-really-possible-for-rand-to-throw-a-zero/28505)._
