# Is this uniformly generating a number from a given interval?

**URL:** <https://discourse.julialang.org/t/is-this-uniformly-generating-a-number-from-a-given-interval/19938>\
**Category:** General Usage\
**Created:** [January 22, 2019, 4:25pm UTC](https://discourse.julialang.org/t/is-this-uniformly-generating-a-number-from-a-given-interval/19938 "2019-01-22T16:25:55Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)\
**Post date:** [January 22, 2019, 4:25pm UTC](https://discourse.julialang.org/t/is-this-uniformly-generating-a-number-from-a-given-interval/19938/1 "2019-01-22T16:25:55Z")

</div>

The following is the code:

```julia
julia> rng = MersenneTwister(0) # here rng is random number generator 
MersenneTwister(UInt32[0x00000000], Base.dSFMT.DSFMT_state(Int32[748398797, 1073523691, -1738140313, 1073664641, -1492392947, 1073490074, -1625281839, 1073254801, 1875112882, 1073717145 … 943540191, 1073626624, 1091647724, 1073372234, -1273625233, -823628301, 835224507, 991807863, 382, 0]), [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 … 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], 382)

julia> rand(rng,[1:100;])
41

```

I understand that MersenneTwister(0) is a random number generator based on MersenneTwister algorithm with seed 0. But how do you understand the above code then?

---

<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:** [January 22, 2019, 5:15pm UTC](https://discourse.julialang.org/t/is-this-uniformly-generating-a-number-from-a-given-interval/19938/2 "2019-01-22T17:15:18Z")

</div>

Yes. That does sample from a uniform pseudorandom distribution in the interval [1,100] inclusive. To get `n` of them, `rand(rng, [1:100;], n)`.

`rand(rng, 1:100)` and `rand(rng, 1:100, n)` also work (better).

---

<div class="post-metadata">

**Author:** ![DrPapa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drpapa/32/6835_2.png) [@DrPapa](https://discourse.julialang.org/u/DrPapa)\
**Post date:** [January 22, 2019, 6:15pm UTC](https://discourse.julialang.org/t/is-this-uniformly-generating-a-number-from-a-given-interval/19938/3 "2019-01-22T18:15:17Z")

</div>

> [@JeffreySarnoff](#):
>
> That does sample from a uniform pseudorandom distribution in the interval [1,100] inclusive.

Are you sure? `rand(rng, L , n)` will draw `n` random values uniformly from the list L. Note that in the example `L = [1:100;]` which is the set of integers `1,2,...99,100`. So, this will only return integers from that set. E.g.

```julia
rand(rng,[1:100;],5)
5-element Array{Int64,1}:
 78
  4
 11
 37
 77

```

In contrast, when I hear “interval [a,b]”, I take that to mean all real numbers R such that a \<= R \<= b. In that case I would using the Distributions.jl package. Here I generate `n` uniform samples on the interval [1,100].

```julia
using Distributions
using Random

n = 5
uni_dist = Uniform(1,100)
rand(uni_dist, n)

```

which gives

```julia
5-element Array{Float64,1}:
71.53459709741146 
48.06173894008397 
 4.006744724443985
62.69941473677736 
28.56740770494225 

```

---

<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:** [January 22, 2019, 6:18pm UTC](https://discourse.julialang.org/t/is-this-uniformly-generating-a-number-from-a-given-interval/19938/4 "2019-01-22T18:18:52Z")

</div>

Usually, in Julia when working with ranges, to indicate a range of integers we use integers and to indicate a range of floats we use floats.  
E.G. `1:100` vs `1.0:<step>:100.0`. Without the step specified, it defaults to one(T) so `1:100` is `1:1:100` and `1.0:100.0` is `1.0:1.0:100.0`.

Yes, I am sure.

---

<div class="post-metadata">

**Author:** ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)\
**Post date:** [January 22, 2019, 6:20pm UTC](https://discourse.julialang.org/t/is-this-uniformly-generating-a-number-from-a-given-interval/19938/5 "2019-01-22T18:20:02Z")

</div>

I believe that’s a concept thing. Anyway, thank you both. @DrPapa, @JeffreySarnoff

---

<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:** [January 22, 2019, 6:22pm UTC](https://discourse.julialang.org/t/is-this-uniformly-generating-a-number-from-a-given-interval/19938/6 "2019-01-22T18:22:38Z")

</div>

> [@bsnyh](#):
>
> rand(rng,[1:100;])

Don’t do this. If you want to sample from the list of integers 1 to 100, do `rand(rng, 1:100)`. Don’t use square brackets, and don’t `collect`. You needlessly allocate a large vector this way, and it gets worse the longer the range.

---

<div class="post-metadata">

**Author:** ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)\
**Post date:** [January 22, 2019, 6:23pm UTC](https://discourse.julialang.org/t/is-this-uniformly-generating-a-number-from-a-given-interval/19938/7 "2019-01-22T18:23:47Z")

</div>

thank you for letting me know. @DNF

---

<div class="post-metadata">

**Author:** ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)\
**Post date:** [January 22, 2019, 6:24pm UTC](https://discourse.julialang.org/t/is-this-uniformly-generating-a-number-from-a-given-interval/19938/8 "2019-01-22T18:24:39Z")

</div>

as the other post mentioned, let’s give ‘collect’ a rather ugly name in case people use it. 😉

---

<div class="post-metadata">

**Author:** ![DrPapa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drpapa/32/6835_2.png) [@DrPapa](https://discourse.julialang.org/u/DrPapa)\
**Post date:** [January 22, 2019, 6:47pm UTC](https://discourse.julialang.org/t/is-this-uniformly-generating-a-number-from-a-given-interval/19938/9 "2019-01-22T18:47:05Z")

</div>

I don’t disagree with what you say regarding ranges. Because the OP asked “Is this uniformly generating a number from a given interval”, I want to be careful and explicit with the word _interval_.

`rand(rng,1:100)` does not sample in the interval [1,100]. It samples from the set of integers 1,2,…99,100. Additionally, `rand(rng,1.0:100.0)` samples from the set of floats 1.0, 2.0,…99.0,100.0 not from the interval [1,100].

When uniformly sampling from the interval [1,100] it would be just as likely to sample 1.0 as it is to sample 1.1. However, that is not the case with `rand(rng,1.0:100.0)`. Here, the probability of sampling 1.0 is 1/100 and the probability of sampling 1.1 is 0.

---

<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:** [January 22, 2019, 7:01pm UTC](https://discourse.julialang.org/t/is-this-uniformly-generating-a-number-from-a-given-interval/19938/10 "2019-01-22T19:01:26Z")

</div>

> [@DrPapa](#):
>
> I want to be careful and explicit with the word _interval_ .

Usage varies, but [integer intervals](https://en.wikipedia.org/wiki/Interval_(mathematics)#Integer_intervals) are also possible.

---

<div class="post-metadata">

**Author:** ![DrPapa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drpapa/32/6835_2.png) [@DrPapa](https://discourse.julialang.org/u/DrPapa)\
**Post date:** [January 22, 2019, 7:08pm UTC](https://discourse.julialang.org/t/is-this-uniformly-generating-a-number-from-a-given-interval/19938/11 "2019-01-22T19:08:59Z")

</div>

Good to know. @JeffreySarnoff My apologies.
