# Random number in (0,1)

**URL:** https://discourse.julialang.org/t/random-number-in-0-1/51451
**Category:** Statistics
**Created:** [December 8, 2020, 11:33am UTC](https://discourse.julialang.org/t/random-number-in-0-1/51451 "2020-12-08T11:33:25Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![jherekhealy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jherekhealy/32/20062_2.png) [@jherekhealy](https://discourse.julialang.org/u/jherekhealy)
#### Post date: [December 8, 2020, 11:33am UTC](https://discourse.julialang.org/t/random-number-in-0-1/51451/1 "2020-12-08T11:33:25Z")

</div>

In Random.jl, there is the possibility to generate a random number in the interval [0,1) through CloseOpen01, or even in [1,2) (I can see the reason for this for implementation, less so for a user) through CloseOpen12, but there is no way to specify OpenOpen01.

Generating in (0,1) is particularly important to simulate various distributions through the inverse cumulative distribution function, a very common technique in Monte-Carlo methods. The dSFMT code provides such methods. Of course, it is always possible to add an if statement around each random number generation, but this is not particularly clean or efficient, plus it will not match the original dSFMT numbers in (0,1).

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [December 8, 2020, 12:01pm UTC](https://discourse.julialang.org/t/random-number-in-0-1/51451/2 "2020-12-08T12:01:17Z")

</div>

I just want to add question, if someone is able to answer you: How important this can be in practice? With a period of \sim 2^{20000}, an implementation with open or closed intervals makes any difference in any actual application?

(I am assuming that this is a question addressed to the generation of a sequence of “continuous” floats, if one is sampling a discrete distribution that might be a problem, but then we only need to adjust the interval of the sequence generated).

Anyway, a simple workaround would be `rnd = nextfloat(0.) + rand()` (which actually does not affect the upper limit, because `1. + nextfloat(0.) == 1.`)

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [December 8, 2020, 12:17pm UTC](https://discourse.julialang.org/t/random-number-in-0-1/51451/3 "2020-12-08T12:17:48Z")

</div>

There is a solution by @Elrod [here](https://discourse.julialang.org/t/how-to-create-a-random-uniform-distribution-between-but-excluding-0-and-10/21908/20) to get the random numbers distribution over (0,1):

```julia
prevfloat(1.0)*(1-rand())

```

---

<div class="post-metadata">

### Author: ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)
#### Post date: [December 8, 2020, 12:20pm UTC](https://discourse.julialang.org/t/random-number-in-0-1/51451/4 "2020-12-08T12:20:24Z")

</div>

> [@lmiq](#):
>
> Anyway, a simple workaround would be `rnd = nextfloat(0.) + rand()` (which actually does not affect the upper limit, because `1. + nextfloat(0.) == 1.` )

That will still skew the result slightly, as it unproportionally increases the likelyhood of 2^{-2021}, but that’s probably fine for most practical uses.

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [December 8, 2020, 12:29pm UTC](https://discourse.julialang.org/t/random-number-in-0-1/51451/5 "2020-12-08T12:29:22Z")

</div>

I think that other thread exhaust the subject. I was thinking only that one might want to avoid `0.` because of numerical instabilities (even if very, very rare), but even for that reason changing that does not make any difference, because

```julia
julia> 1. / nextfloat(0.) == 1. / 0. == Inf
true

```

---

<div class="post-metadata">

### Author: ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)
#### Post date: [December 8, 2020, 12:29pm UTC](https://discourse.julialang.org/t/random-number-in-0-1/51451/6 "2020-12-08T12:29:35Z")

</div>

Currently, I’m subtracting the following from the `[1,2)` random number in [VectorizedRNG.jl](https://github.com/chriselrod/VectorizedRNG.jl/blob/e7915151d5e3c6a4f993d12cc8b68aafdbbd2854/src/api.jl#L68):

```julia
oneopenconst(::Type{Float64}) = 0.9999999999999999
oneopenconst(::Type{Float32}) = 0.99999994f0

```

This provides:

```julia
julia> 1 - oneopenconst(Float64)
1.1102230246251565e-16

julia> 1 - oneopenconst(Float32)
5.9604645f-8

julia> prevfloat(2.0) - oneopenconst(Float64)
0.9999999999999999

julia> prevfloat(2f0) - oneopenconst(Float32)
0.99999994f0

```

---

<div class="post-metadata">

### Author: ![jherekhealy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jherekhealy/32/20062_2.png) [@jherekhealy](https://discourse.julialang.org/u/jherekhealy)
#### Post date: [December 9, 2020, 10:27am UTC](https://discourse.julialang.org/t/random-number-in-0-1/51451/7 "2020-12-09T10:27:52Z")

</div>

I could see many use cases where this nextfloat(0) will be problematic. The Inverse CDF of this number is just typically huge and will likely strongly skew results of a Monte-Carlo simulation

---

<div class="post-metadata">

### Author: ![jherekhealy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jherekhealy/32/20062_2.png) [@jherekhealy](https://discourse.julialang.org/u/jherekhealy)
#### Post date: [December 9, 2020, 10:29am UTC](https://discourse.julialang.org/t/random-number-in-0-1/51451/8 "2020-12-09T10:29:55Z")

</div>

This will clearly break any Monte-Carlo simulation relying on an inverse cdf as invcdf(0) = -Inf.  
The result of the simulation will then be Inf or NaN.

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [December 9, 2020, 10:34am UTC](https://discourse.julialang.org/t/random-number-in-0-1/51451/9 "2020-12-09T10:34:00Z")

</div>

The first float after `0.` is `5.0e-324`, while you get `1/x = Inf` for numbers up to about `1e-309`. So the problem, if it is there, is not only about the open or closed intervals, but defining the inverse of these number in a safe way.

---

<div class="post-metadata">

### Author: ![jherekhealy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jherekhealy/32/20062_2.png) [@jherekhealy](https://discourse.julialang.org/u/jherekhealy)
#### Post date: [December 9, 2020, 10:38am UTC](https://discourse.julialang.org/t/random-number-in-0-1/51451/10 "2020-12-09T10:38:22Z")

</div>

@Elrod I like your idea best. I suppose we loose a tiny bit of accuracy.  
It also does not break any symmetry and is fast:

1-((2-eps(Float64)) - oneopenconst(Float64)) = 1 - oneopenconst(Float64)
