# How to create an array of specific length populated by random uniform values within a range

**URL:** <https://discourse.julialang.org/t/how-to-create-an-array-of-specific-length-populated-by-random-uniform-values-within-a-range/22201>\
**Category:** New to Julia\
**Created:** [March 22, 2019, 2:47pm UTC](https://discourse.julialang.org/t/how-to-create-an-array-of-specific-length-populated-by-random-uniform-values-within-a-range/22201 "2019-03-22T14:47:30Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [March 22, 2019, 2:47pm UTC](https://discourse.julialang.org/t/how-to-create-an-array-of-specific-length-populated-by-random-uniform-values-within-a-range/22201/1 "2019-03-22T14:47:30Z")

</div>

Hello, beginner Julia user here.

This is a continuation of [a previous post](https://discourse.julialang.org/t/how-to-create-a-random-uniform-distribution-between-but-excluding-0-and-10/21908) where I wanted to create a random uniform distribution between 0 (which is excluded) and 10 (included). I am making a second post but keeping the range general in the hope it will help someone one day.

So for example:  
I would like an array of length `8`  
In this array I would like to randomly generate `8` values  
These values fall uniformly within the ranges of `2` and `7`.

I guess this could also be phrased as,  
How to generate 8 random uniform values between 2 and 7, and place them within an array?

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [March 22, 2019, 2:53pm UTC](https://discourse.julialang.org/t/how-to-create-an-array-of-specific-length-populated-by-random-uniform-values-within-a-range/22201/2 "2019-03-22T14:53:54Z")

</div>

```julia
5 .* rand(8) .+ 2

```

(broadcasting the multiplication as well makes it a tiny bit faster)

Here is the [documentation on broadcasting](https://docs.julialang.org/en/v1/base/arrays/#Broadcast-and-vectorization-1) btw.

---

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [March 22, 2019, 2:56pm UTC](https://discourse.julialang.org/t/how-to-create-an-array-of-specific-length-populated-by-random-uniform-values-within-a-range/22201/3 "2019-03-22T14:56:49Z")

</div>

Thank you for your reply.

What does the 5 and +2 represent?

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [March 22, 2019, 3:00pm UTC](https://discourse.julialang.org/t/how-to-create-an-array-of-specific-length-populated-by-random-uniform-values-within-a-range/22201/4 "2019-03-22T15:00:49Z")

</div>

Another way of thinking of uniformly distributed random values between 2 and 7 is uniformly distributed random values between 0 and 5 and then adding 2 to those. The `rand` function generates values in `[0, 1]`, so if you multiply that by 5, you get values in `[0, 5]`.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 22, 2019, 3:15pm UTC](https://discourse.julialang.org/t/how-to-create-an-array-of-specific-length-populated-by-random-uniform-values-within-a-range/22201/5 "2019-03-22T15:15:00Z")

</div>

> [@ExpandingMan](#):
>
> `5 .* rand(8) .+ 2`

You could also do `[5rand()+2 for i=1:8]` to eliminate the temporary `rand(8)` array.

Alternatively, using the [Distributions.jl package](https://github.com/JuliaStats/Distributions.jl), you can do

```julia
using Distributions
rand(Uniform(2,7), 8)

```

which is maybe a bit clearer, and generalizes better to other types of distributions if you need that flexibility.

---

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [March 22, 2019, 10:01pm UTC](https://discourse.julialang.org/t/how-to-create-an-array-of-specific-length-populated-by-random-uniform-values-within-a-range/22201/6 "2019-03-22T22:01:23Z")

</div>

> [@HelgavonLichtenstein](#):
>
> So for example:  
> I would like an array of length `8`  
> In this array I would like to randomly generate `8` values  
> These values fall uniformly within the ranges of `2` and `7` .

This is how you should think. (pardon me english)

1. The range of value is from 2 to 7 therefore the span is 7 - 2 = 5.

2. The offset is 2 because the smallest number is 2.

3. Create a random number generator with span 5 and offset 2

```julia
julia> function myrng()
       return 2.0 + 5.0 * rand()
       end
myrng (generic function with 1 method)

```

1. finally create your array

```julia
julia> a = [myrng() for n in 1:8]
8-element Array{Float64,1}:
 4.636049027494998
 5.061366873363691
 6.420185007452647
 6.955145662491416
 2.708159366136055
 6.223899415297151
 6.682556947797874
 4.927220645178398

```

or you can use

```julia
julia> 5.0 .* rand(8) .+ 2.0
8-element Array{Float64,1}:
 2.2677134636706024
 6.9686960119949095
 5.713372377688906 
 4.253700066196062 
 3.483397341546835 
 4.291678326242685 
 3.094738077434428 
 6.920131322028918

```

Any in case you are curious, this could come out with the exact number 2.0 but will NEVER come out with the exact number 7.0

If you want to include both 2.0 and 7.0 then replace rand() with rand\_ZeroAndOneIncluded()

```julia
julia> function rand_ZeroAndOneIncluded()
       return rand()/prevfloat(1.0)
       end
rand_ZeroAndOneIncluded (generic function with 1 method)

```

---

<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:** [March 23, 2019, 2:31pm UTC](https://discourse.julialang.org/t/how-to-create-an-array-of-specific-length-populated-by-random-uniform-values-within-a-range/22201/7 "2019-03-23T14:31:18Z")

</div>

> [@HelgavonLichtenstein](#):
>
> hope it will help someone one day

If this is a common problem, then perhaps the best way to help others is to make a small PR to `Distributions` with a variant of `Uniform` that does this. Or even generalizing the idea to other (half)bounded distributions.

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [March 23, 2019, 7:42pm UTC](https://discourse.julialang.org/t/how-to-create-an-array-of-specific-length-populated-by-random-uniform-values-within-a-range/22201/8 "2019-03-23T19:42:38Z")

</div>

I recall from your previous question that there was a big discussion on your requirement that you want a random number in the range `(F1,F2]`, in other words that `F1` is excluded and `F2` included. The “problem” is that `rand()` defaults to producing numbers in the range `[0,1)`, i.e., lower bound _included_ and upper bound _excluded_. In that thread, someone suggested that `1-rand()` produces numbers in the range `(0,1]`. Thus, I think you can do:

```nohighlight
function my_rand(F1,F2,dims...)
    return F1 .+ (F2-F1)*(1 .- rand(dims...))
end

```

If you want a function that returns a scalar when `dims` is skipped, you can add the method:

```nohighlight
function my_rand(F1,F2)
    return F1 + (F2-F1)*(1-rand())
end

```

A couple of examples (assuming the functions have been defined):

```nohighlight
julia> my_rand(2,7,8)
8-element Array{Float64,1}:
 6.826654827094337
 6.4343778115292025
 2.078598954398351
 5.963089683352704
 6.729404550779482
 6.2861095743137465
 5.70630650452843
 3.127727841177605

julia> my_rand(2,7,2,3)
2×3 Array{Float64,2}:
 4.41794 5.86907 2.04626
 6.27201 3.7892 2.90218

julia> my_rand(2,7)
5.92523692894661

```

If you want efficient code, some of the other suggestions may be better.

---

<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:** [March 27, 2019, 11:44am UTC](https://discourse.julialang.org/t/how-to-create-an-array-of-specific-length-populated-by-random-uniform-values-within-a-range/22201/9 "2019-03-27T11:44:02Z")

</div>

> [@StevenSiew](#):
>
> If you want to include both 2.0 and 7.0 then replace rand() with rand\_ZeroAndOneIncluded()

Actually, as the last bit of a value produced by `rand` is `0`, you need to call `rand() / prevfloat(1.0, 2)` in order to have `1.0` included in the possible outcomes.

---

<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:** [March 27, 2019, 11:55am UTC](https://discourse.julialang.org/t/how-to-create-an-array-of-specific-length-populated-by-random-uniform-values-within-a-range/22201/10 "2019-03-27T11:55:50Z")

</div>

Stepping back a bit from the immediate problem, the best approach may not be designing a function that can ensure random draws \in (a, b], but an algorithm that can deal with =a robustly.

It is common to have modeling problems where eg some x \in D holds for an open subset of \mathbb{R}^n. Then for continuous functions f\_i, it of course follows that f\_1(x) \in f\_1(D), f\_2(f\_1(x)) \in f\_2(f\_1(D)), etc.

But for floating point, numerical error will inevitably creep in, especially for complex transformations. At that point the algorithm should be able to cope with x \in D but f\_n(\dots) being “invalid”, eg

1. by returning a `-Inf` log likelihood/posterior for likelihood-based inference,
2. by not recording a simulated sample and trying again for MC simulation,
3. “regularizing” the value so that it is valid (eg for x \< a, replace with a+\epsilon, this is sometimes not innocuous though).
