# 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:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 9, 2019, 8:24am UTC](https://discourse.julialang.org/t/is-it-really-possible-for-rand-to-throw-a-zero/28505/21 "2019-09-09T08:24:15Z")

</div>

> [@Per](#):
>
> Far more common than I would have guessed…

Think of it as an RNG thrown in for free 😉

---

<div class="post-metadata">

**Author:** ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)\
**Post date:** [September 9, 2019, 10:13am UTC](https://discourse.julialang.org/t/is-it-really-possible-for-rand-to-throw-a-zero/28505/22 "2019-09-09T10:13:27Z")

</div>

Yes, and the difference between exact zero and the next number in line might be an index error which crashes the program or not.

Think of binning data and 0.0 falls into the "zero"th bin:

```julia
julia> a = 'A':'Z'
'A':1:'Z'

julia> a[ceil(Int, rand() * length(a))]
'K'

```

versus (if you are unlucky)

```julia
julia> a[ceil(Int, (1-rand()) * length(a))]
ERROR: BoundsError: attempt to access 26-element StepRange{Char,Int64} at index [0]

```

---

<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 9, 2019, 10:41am UTC](https://discourse.julialang.org/t/is-it-really-possible-for-rand-to-throw-a-zero/28505/23 "2019-09-09T10:41:06Z")

</div>

In Julia, you’d write `rand(a)` rather than `a[ceil(Int, rand() * length(a))]`, so this is more of a problem in other languages.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 9, 2019, 10:47am UTC](https://discourse.julialang.org/t/is-it-really-possible-for-rand-to-throw-a-zero/28505/24 "2019-09-09T10:47:54Z")

</div>

Or if one absolutely wants the index, `rand(eachindex(a))`.

---

<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 9, 2019, 11:02am UTC](https://discourse.julialang.org/t/is-it-really-possible-for-rand-to-throw-a-zero/28505/25 "2019-09-09T11:02:47Z")

</div>

The `1/x` case (mentioned somewhere above) is interesting, because it is not sufficient to simply filter out `0.0`. If the zero is important enough to matter, then the interval between zero and `eps(T)` is probably also important, but rand(T) will only return multiples of `eps(T)`.

I think it’d be fairly straightforward to write a `rand` function which might return any representable float in [0,1], so that the probability of getting a zero is equal to `nextfloat(zero(T))`, i.e. 5.0e-324 for `Float64`. A naïve implementation would run at about half the pace of the standard random number generator, but it might be possible to make it almost as fast.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [September 9, 2019, 11:49am UTC](https://discourse.julialang.org/t/is-it-really-possible-for-rand-to-throw-a-zero/28505/26 "2019-09-09T11:49:01Z")

</div>

Returning subnormals from rand is likely to slow things down on many microprocessors.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [September 9, 2019, 11:53am UTC](https://discourse.julialang.org/t/is-it-really-possible-for-rand-to-throw-a-zero/28505/27 "2019-09-09T11:53:24Z")

</div>

> [@robsmith11](#):
>
> then the probability of rand() producing an exact zero is likely far less than other important bits getting flipped in non-ECC memory

Are you distinguishing “other important bits” from other bits in unused portions of memory?

---

<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 9, 2019, 12:21pm UTC](https://discourse.julialang.org/t/is-it-really-possible-for-rand-to-throw-a-zero/28505/28 "2019-09-09T12:21:58Z")

</div>

Only about one in 10^308 returned numbers would be subnormal, so it would not affect performance in any measurable way. (Of course, that’s also a reason not to bother with returning subnormal numbers if it requires any extra coding effort.)

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 9, 2019, 12:34pm UTC](https://discourse.julialang.org/t/is-it-really-possible-for-rand-to-throw-a-zero/28505/29 "2019-09-09T12:34:29Z")

</div>

> [@Per](#):
>
> The `1/x` case (mentioned somewhere above) is interesting, because it is not sufficient to simply filter out `0.0` . If the zero is important enough to matter, then the interval between zero and `eps(T)` is probably also important, but rand(T) will only return multiples of `eps(T)` .

For most practical problems I can think of where 1/x is needed, either one uses a bounded f(1/x) (so that the expectation is finite) and/or the method is ill-behaved anyway so that one would use a transformation. I think that it is important to consider only examples where expected values a finite and preferably second moments also exist, otherwise MC is a dubious proposition anyway.

But that’s a red herring: if think the _Julian_ solution is to implement a sampler for a custom `Open01` object, and then the semantics are clear. This could of course live in a package.

---

<div class="post-metadata">

**Author:** ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)\
**Post date:** [September 9, 2019, 1:30pm UTC](https://discourse.julialang.org/t/is-it-really-possible-for-rand-to-throw-a-zero/28505/30 "2019-09-09T13:30:42Z")

</div>

No… This is a simplified example how an index error might happen. Not an example how to draw a random element from a vector (yes, you wouldn’t do it this way.)

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [September 9, 2019, 4:28pm UTC](https://discourse.julialang.org/t/is-it-really-possible-for-rand-to-throw-a-zero/28505/31 "2019-09-09T16:28:05Z")

</div>

Another possibility for getting (0,1] is

```nohighlight
openclosed(t...) = (r = rand(t...); iszero(r) ? one(r) : r)

```

which simply switches out the zero value with one.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [September 9, 2019, 4:36pm UTC](https://discourse.julialang.org/t/is-it-really-possible-for-rand-to-throw-a-zero/28505/32 "2019-09-09T16:36:38Z")

</div>

1-rand() will probably be faster.

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [September 9, 2019, 4:44pm UTC](https://discourse.julialang.org/t/is-it-really-possible-for-rand-to-throw-a-zero/28505/33 "2019-09-09T16:44:12Z")

</div>

```nohighlight
julia> @btime openclosed()
  3.848 ns (0 allocations: 0 bytes)
0.33216440795825175

julia> @btime y() # 1-rand(t...)
  3.657 ns (0 allocations: 0 bytes)
0.04763324146749448

```

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [September 9, 2019, 4:53pm UTC](https://discourse.julialang.org/t/is-it-really-possible-for-rand-to-throw-a-zero/28505/34 "2019-09-09T16:53:53Z")

</div>

How do the timings compare for `openclosed(n)` and `y(n)` where n is 100, 1\_000, 10\_000?

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [September 9, 2019, 5:16pm UTC](https://discourse.julialang.org/t/is-it-really-possible-for-rand-to-throw-a-zero/28505/35 "2019-09-09T17:16:25Z")

</div>

`y(n)` errors out, while `openclosed(100)` is valid code

---

<div class="post-metadata">

**Author:** ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)\
**Post date:** [September 9, 2019, 5:18pm UTC](https://discourse.julialang.org/t/is-it-really-possible-for-rand-to-throw-a-zero/28505/36 "2019-09-09T17:18:48Z")

</div>

Ideally we could have a `RandomMode` like we have a `RoundingMode`, no?

---

<div class="post-metadata">

**Author:** ![rfourquet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rfourquet/32/3610_2.png) [@rfourquet](https://discourse.julialang.org/u/rfourquet)\
**Post date:** [September 9, 2019, 5:35pm UTC](https://discourse.julialang.org/t/is-it-really-possible-for-rand-to-throw-a-zero/28505/37 "2019-09-09T17:35:03Z")

</div>

> [@aerdely](#):
>
> Is `rand` ever throwing a zero?

Like others said, yes, and this definition doesn’t apply only to `Float64`, so it’s not only theoretical. For example, the array `rand(Float16, 2^12)` has a pretty good chance to contain `0`.

One pretty efficient solution to generate in `(0, 1)` is to set the least significant bit to `1` (which is always set to `0` by default), e.g. `reinterpret(Float64, reinterpret(UInt64, rand(rng, Float64)) | 0x0000000000000001)`. I believe this should keep the distribution mostly uniform. This is what the `RandomExtensions.jl` package does (it provides few related distributions, like `OpenOpen`, `CloseOpen` etc.).

---

<div class="post-metadata">

**Author:** ![aerdely](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aerdely/32/37506_2.png) [@aerdely](https://discourse.julialang.org/u/aerdely)\
**Post date:** [September 9, 2019, 6:23pm UTC](https://discourse.julialang.org/t/is-it-really-possible-for-rand-to-throw-a-zero/28505/38 "2019-09-09T18:23:07Z")

</div>

From that point of view then the best choice would be [0,1] not [0,1) because it is easier to discard 1 and 0.

---

<div class="post-metadata">

**Author:** ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)\
**Post date:** [September 9, 2019, 7:30pm UTC](https://discourse.julialang.org/t/is-it-really-possible-for-rand-to-throw-a-zero/28505/39 "2019-09-09T19:30:37Z")

</div>

It would be easier to discard 1 and 0, but it has other advantages to let vanilla `rand()` fit into a half open interval.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 9, 2019, 7:45pm UTC](https://discourse.julialang.org/t/is-it-really-possible-for-rand-to-throw-a-zero/28505/40 "2019-09-09T19:45:55Z")

</div>

As I said above, it would probably make sense to customize this with just different samples — there is no universal _best_ choice.

That said, if you look at the source, drawing a float in [0, 1) can be done with a some rather elegant bit-fiddling, so in that sense it is rather special and reasonable as a default.

[Previous page](https://discourse.julialang.org/t/is-it-really-possible-for-rand-to-throw-a-zero/28505.md?page=1)

[Next page](https://discourse.julialang.org/t/is-it-really-possible-for-rand-to-throw-a-zero/28505.md?page=3)
