Is this uniformly generating a number from a given interval?

The following is the code:

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;])

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?

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).

1 Like

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.

5-element Array{Int64,1}:

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].

using Distributions
using Random

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

which gives

5-element Array{Float64,1}:
1 Like

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.


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

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.


thank you for letting me know. @DNF

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

1 Like

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.

Usage varies, but integer intervals are also possible.

Good to know. @JeffreySarnoff My apologies.

1 Like