# Random number in (0,1\]

**URL:** https://discourse.julialang.org/t/random-number-in-0-1/16009
**Category:** General Usage
**Created:** [October 8, 2018, 6:14am UTC](https://discourse.julialang.org/t/random-number-in-0-1/16009 "2018-10-08T06:14:34Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)
#### Post date: [October 8, 2018, 6:14am UTC](https://discourse.julialang.org/t/random-number-in-0-1/16009/1 "2018-10-08T06:14:34Z")

</div>

I want a random `Float64` in the interval `(0,1]`.  
I understand `rand()` returns a number in `[0,1)`.  
Should I just use `1-rand()` ?

---

<div class="post-metadata">

### Author: ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)
#### Post date: [October 8, 2018, 6:31am UTC](https://discourse.julialang.org/t/random-number-in-0-1/16009/2 "2018-10-08T06:31:52Z")

</div>

is `rand()` not more like `(0,1)`? And how much does `[0,1)` matter vs `(0,1]`? For most numerical problems, I assume the distinct makes no different what-so-ever in practice.

---

<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: [October 8, 2018, 7:16am UTC](https://discourse.julialang.org/t/random-number-in-0-1/16009/3 "2018-10-08T07:16:03Z")

</div>

> [@greg\_plowman](#):
>
> Should I just use `1-rand()` ?

That’s a nice and quick solution. You can also check for `1` and make it into a `0`, eg

```julia
rand(Float64) |> x -> iszero(x) ? one(x) : x

```

---

<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: [October 8, 2018, 7:37am UTC](https://discourse.julialang.org/t/random-number-in-0-1/16009/4 "2018-10-08T07:37:22Z")

</div>

You should not use `1.0 - rand()`.  
The density of Float64s (how many occur within a subspan) varies within [0.0,1.0].

 ![line_thingy_2](https://global.discourse-cdn.com/julialang/original/3X/c/d/cd81287ba433098b050340cbc2530946c41a42fd.png)  
(source: [Float](https://ridiculousfish.com/blog/posts/float.html))

![fig1](https://global.discourse-cdn.com/julialang/original/3X/5/7/57d596f03867c31f5ef8019a31c1ebed36063f9e.jpeg)  
(source: [Luis R. Izquierdo and J. Gary Polhill: Is Your Model Susceptible to Floating-Point Errors?](http://jasss.soc.surrey.ac.uk/9/4/4.html))

for a single random Float64 in (0.0, 1.0]

```julia
randfloat = rand()
if randfloat === 0.0
    randfloat = 1.0
end

```

for n of them

```julia
zerosasones(x) = ifelse(x === 0.0, 1.0, x) 
randfloats = zerosasones.(rand(n))

```

---

<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: [October 8, 2018, 7:56am UTC](https://discourse.julialang.org/t/random-number-in-0-1/16009/5 "2018-10-08T07:56:21Z")

</div>

> [@JeffreySarnoff](#):
>
> The density of Float64s (how many occur within a subspan) varies within [0.0,1.0].

I am not sure how that is relevant, AFAIK `rand` accounts for that and the result is uniform.

---

<div class="post-metadata">

### Author: ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)
#### Post date: [October 8, 2018, 8:06am UTC](https://discourse.julialang.org/t/random-number-in-0-1/16009/6 "2018-10-08T08:06:02Z")

</div>

The density is varying but `rand()` does sample from it uniformly as it actually samples from `[1,2[` and subtracts `1.0`.

This is equivalent to running `2.0-rand(Random.CloseOpen12())` which avoids this subtraction.

You can check it by running:

```julia
julia> function t(m1, m2)
           x = 1.0 - rand(m1)
               y = 2.0 - rand(m2, Random.CloseOpen12())
                   x == y
                   end
t (generic function with 2 methods)

julia> m1, m2 = MersenneTwister(100), MersenneTwister(100);

julia> all(t(m1,m2) for i in 1:10^8)
true

```

The key thing here is that on `[1,2[` interval all `Float64` are uniform. You can check that `eps` for any value in `[1,2[` range is the same. as well as the difference between `x` and `nextfloat(x)` in this range.

In other words if you look at [Luis R. Izquierdo and J. Gary Polhill: Is Your Model Susceptible to Floating-Point Errors?](http://jasss.soc.surrey.ac.uk/9/4/4.html) image `[1,2[` just happens to be one subsegments that is uniform on it.

Actually on [Float](https://ridiculousfish.com/blog/posts/float.html) it is shown on this picture:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/a/a/aa30bd0307ad87aa8bf26131e45931c903a3b20d.png)

---

<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: [October 8, 2018, 8:13am UTC](https://discourse.julialang.org/t/random-number-in-0-1/16009/7 "2018-10-08T08:13:23Z")

</div>

```julia
using StatsBase: skewness

rs01 = rand(n)
rs10 = 1.0 .- rs01

skewness(rs01) == -skewness(rs10)

```

---

<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: [October 8, 2018, 8:15am UTC](https://discourse.julialang.org/t/random-number-in-0-1/16009/8 "2018-10-08T08:15:16Z")

</div>

You are demonstrating numerical error (after correcting to `rs10 = 1.0 .- rs01`, which makes it run).

---

<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: [October 8, 2018, 8:18am UTC](https://discourse.julialang.org/t/random-number-in-0-1/16009/9 "2018-10-08T08:18:12Z")

</div>

I am demonstrating skewness in small random samples and that it swaps signs using `1.0 .- rand()`

More to the point, a uniform random number generator returning values in [0.0, 1.0) is used internally in simulation code that requires random variates from some specific non-unform distribution, often also in [0.0, 1.0). Once that happens, using one minus random variate probably maps some distinct smaller numbers together as they cross power of 2 bounds.

---

<div class="post-metadata">

### Author: ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)
#### Post date: [October 8, 2018, 8:24am UTC](https://discourse.julialang.org/t/random-number-in-0-1/16009/10 "2018-10-08T08:24:25Z")

</div>

What you show is important, but it is unrelated to random number generation. If you take any vector `x` (random or not) you will get the same result. What I understand @Tamas_Papp says is that `skewness(1.0 .- x) == skewness(x)` will be often false due to roundoff errors.

---

<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: [October 8, 2018, 8:25am UTC](https://discourse.julialang.org/t/random-number-in-0-1/16009/11 "2018-10-08T08:25:54Z")

</div>

The swapping of signs in skewness is not a function of roundoff errors, it is a function of the mirroring.

---

<div class="post-metadata">

### Author: ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)
#### Post date: [October 8, 2018, 8:26am UTC](https://discourse.julialang.org/t/random-number-in-0-1/16009/12 "2018-10-08T08:26:02Z")

</div>

> [@JeffreySarnoff](#):
>
> Once that happens

This is correct - if you have a non-uniform distribution on `[0,1[` then taking `1` minus it might hit the non uniformity problem.

---

<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: [October 8, 2018, 8:27am UTC](https://discourse.julialang.org/t/random-number-in-0-1/16009/13 "2018-10-08T08:27:15Z")

</div>

> [@JeffreySarnoff](#):
>
> I am demonstrating skewness in small random samples and that it swaps signs using `1.0 .- rand()`

No, as s(x) = -s(A-x) is an identity for s being `skewness`, and A a constant. What you are showing is that this does not hold in practice, because of numerical error. That is orthogonal to this discussion. Cf eg

```julia
julia> x = rand(10^9);

julia> A = 100
100

julia> sum(x) + A == sum(x .+ A)
false

```

and various similar examples.

---

<div class="post-metadata">

### Author: ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)
#### Post date: [October 8, 2018, 8:28am UTC](https://discourse.julialang.org/t/random-number-in-0-1/16009/14 "2018-10-08T08:28:06Z")

</div>

I am referring to:

```julia
julia> using Random

julia> Random.seed!(100);

julia> x = rand(100000);

julia> skewness(1.0 .- x) == -skewness(x)
false

```

and you see that the sign is swapped but the values are not exactly opposite - they are only approximately opposite.

---

<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: [October 8, 2018, 8:29am UTC](https://discourse.julialang.org/t/random-number-in-0-1/16009/15 "2018-10-08T08:29:09Z")

</div>

this is getting away from itself – try the same thing using Float16 rands, Float16(skewness(x)) and 1000 samples. I know that floating point errors accrue – no argument there. 😉

_coding to the internal workings of rand() … kinda iffy for FAA system simulations_

---

<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: [October 8, 2018, 10:57am UTC](https://discourse.julialang.org/t/random-number-in-0-1/16009/16 "2018-10-08T10:57:58Z")

</div>

So you’re saying that if you take `1.0 - rand()` then many of the small floats, close to zero, can _never_ be selected, since `eps` is larger close to 1.0? Those small numbers just become inaccessible to the algorithm.

---

<div class="post-metadata">

### Author: ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)
#### Post date: [October 8, 2018, 11:27am UTC](https://discourse.julialang.org/t/random-number-in-0-1/16009/17 "2018-10-08T11:27:02Z")

</div>

Yes. You can theoretically get `0` and the next theoretically possible value is `eps()`.
