# Reaching bounds of Distributions.Uniform()

**URL:** https://discourse.julialang.org/t/reaching-bounds-of-distributions-uniform/60956
**Category:** Statistics
**Created:** [May 11, 2021, 2:40pm UTC](https://discourse.julialang.org/t/reaching-bounds-of-distributions-uniform/60956 "2021-05-11T14:40:26Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![goualard-f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goualard-f/32/23904_2.png) [@goualard-f](https://discourse.julialang.org/u/goualard-f)
#### Post date: [May 11, 2021, 2:40pm UTC](https://discourse.julialang.org/t/reaching-bounds-of-distributions-uniform/60956/1 "2021-05-11T14:40:26Z")

</div>

The documentation for [`Distributions.Uniform()`](https://juliastats.org/Distributions.jl/latest/univariate/#Distributions.Uniform) states that it allows to draw a value in `[a,b]` (bounds included). Looking into [the code](https://github.com/JuliaStats/Distributions.jl/blob/master/src/univariate/continuous/uniform.jl#L112), it seems that the `rand()` function from `Base` is used along with some affine transformation. Since `rand()` returns a value in `[0,1)` (right bound excluded), I am curious to know how `Uniform()` translates that to a closed interval. Or is it defined in that way only to take into account possible rounding errors?

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [May 11, 2021, 3:20pm UTC](https://discourse.julialang.org/t/reaching-bounds-of-distributions-uniform/60956/2 "2021-05-11T15:20:09Z")

</div>

You make a good point, but I don’t see anywhere that the documentation explicitly says that the bounds are included.

A uniform distribution has zero probability of producing either bound.

---

<div class="post-metadata">

### Author: ![goualard-f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goualard-f/32/23904_2.png) [@goualard-f](https://discourse.julialang.org/u/goualard-f)
#### Post date: [May 11, 2021, 3:51pm UTC](https://discourse.julialang.org/t/reaching-bounds-of-distributions-uniform/60956/3 "2021-05-11T15:51:30Z")

</div>

@dpsanders The documentations states:

```julia
Uniform() # Uniform distribution over [0, 1]
Uniform(a, b) # Uniform distribution over [a, b]

```

My understanding is that values returned are taken from the _closed_ intervals `[0,1]` and `[a,b]` respectively.

> [@dpsanders](#):
>
> A uniform distribution has zero probability of producing either bound.

Except we know that we are not really drawing from a continuous uniform distribution. The probability of returning any discrete value from the interval is small but not zero, and it gets larger the smaller the size of the floating-point type considered.

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [May 11, 2021, 3:57pm UTC](https://discourse.julialang.org/t/reaching-bounds-of-distributions-uniform/60956/4 "2021-05-11T15:57:32Z")

</div>

As you say, there is a disconnect between the mathematical concept, as used in “Uniform distribution over [0, 1]” with a closed interval, and the numerical reality.

It seems difficult to believe that including or not the right end-point will affect anything much statistically speaking, even with `Float16`.

[Although a friend once had a bug that cropped up only after very long simulations, due to the fact thatthe right end-point _was_ being produced in his case. It was then processed in a way that led to some kind of nonsensical calculation like `log(0)`.]

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [May 11, 2021, 4:05pm UTC](https://discourse.julialang.org/t/reaching-bounds-of-distributions-uniform/60956/5 "2021-05-11T16:05:07Z")

</div>

See also previous discussions such as

> [@How to create a random Uniform Distribution between (but excluding) 0 and 10?](https://discourse.julialang.org/t/how-to-create-a-random-uniform-distribution-between-but-excluding-0-and-10/21908):
>
> Hello, How can I create a random uniform distribution between 0 (which is excluded) and 10 (included). That is, the range is between 0 and 10, but 0 itself cannot be included. using Distributions rand(Uniform(0, 10) Using rand(Uniform(\>0, 10) does not work.

> <https://github.com/JuliaLang/julia/pull/33251>
>
> Fixes #33222.
> 
> Also changed some hardcoded test values accordingly.
