# Randomly generate a sequence of numbers one by one using the rand function in Distributions module

**URL:** <https://discourse.julialang.org/t/randomly-generate-a-sequence-of-numbers-one-by-one-using-the-rand-function-in-distributions-module/23022>\
**Category:** General Usage\
**Created:** [April 11, 2019, 7:37am UTC](https://discourse.julialang.org/t/randomly-generate-a-sequence-of-numbers-one-by-one-using-the-rand-function-in-distributions-module/23022 "2019-04-11T07:37:09Z")\
**Posts on this page:** 18\
**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:** [April 11, 2019, 7:37am UTC](https://discourse.julialang.org/t/randomly-generate-a-sequence-of-numbers-one-by-one-using-the-rand-function-in-distributions-module/23022/1 "2019-04-11T07:37:09Z")

</div>

Hi, the `random` function in the `Distributions` module is very useful for generating a sequence / vector / list of random numbers one by one based on a given random number generator and a statistical distribution. What I need is to generate a sequence of random numbers (say, several millions) one by one and parse it one by one. I can simply generate it by using the random function call, and save the generated random number sequence into a vector. But this may not seem efficient. I was wondering, is there any other way to handle this? Many thanks.

---

<div class="post-metadata">

**Author:** ![zgornel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zgornel/32/217487_2.png) [@zgornel](https://discourse.julialang.org/u/zgornel)\
**Post date:** [April 11, 2019, 10:39am UTC](https://discourse.julialang.org/t/randomly-generate-a-sequence-of-numbers-one-by-one-using-the-rand-function-in-distributions-module/23022/2 "2019-04-11T10:39:31Z")

</div>

you could use a generator

```julia
n = 1_000_000
rg = (rand() for _ in 1:n)
for r in rg
    #do stuff with random number `r`
end
```

---

<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:** [April 11, 2019, 1:03pm UTC](https://discourse.julialang.org/t/randomly-generate-a-sequence-of-numbers-one-by-one-using-the-rand-function-in-distributions-module/23022/3 "2019-04-11T13:03:51Z")

</div>

Or just a loop?

```julia
for i = 1:n 
    r = rand()
    do_something(r)
end

```

There’s nothing wrong with loops in Julia, unlike other languages where you have to avoid them in critical code.

---

<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:** [April 11, 2019, 1:11pm UTC](https://discourse.julialang.org/t/randomly-generate-a-sequence-of-numbers-one-by-one-using-the-rand-function-in-distributions-module/23022/4 "2019-04-11T13:11:27Z")

</div>

Thank you for kind reply. The situation I’m facing is that, the `rand()` function in the `Distributions` module can be optionally supplied with a default first parameter which indicates the random number generator. If I put this rand function with the supplied random number generator, say `MersenneTwister(4)`, then the rand function will give me the “same” number each time, which is not what I need. Moreover, I need more than one rand function calls like this inside of the same loop. Do you know how to accomplish this? I eventually generate the random numbers I need and save them in a vector and then parse it one by one inside of the for loop. I wonder if there is a better way for this.

---

<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:** [April 11, 2019, 1:14pm UTC](https://discourse.julialang.org/t/randomly-generate-a-sequence-of-numbers-one-by-one-using-the-rand-function-in-distributions-module/23022/5 "2019-04-11T13:14:54Z")

</div>

> [@bsnyh](#):
>
> If I put this rand function with the supplied random number generator, say `MersenneTwister(4)` , then the rand function will give me the “same” number each time

I’m not sure to understand, but it will be true only if you put the `MersenneTwister(4)` call within the loop. Just assign the rng to a variable before the loop:

```julia
rng = MersenneTwister(4) # or rng = MersenneTwister() for a random seed
for i = 1:n 
    r = rand(rng) # different value at each iteration
    do_something(r)
end
```

---

<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:** [April 11, 2019, 2:17pm UTC](https://discourse.julialang.org/t/randomly-generate-a-sequence-of-numbers-one-by-one-using-the-rand-function-in-distributions-module/23022/6 "2019-04-11T14:17:03Z")

</div>

Thank you for your reply. The thing is, I need more than one `rand` function calls inside of the for loop, and to make it more challenging, for each `rand` function call, I need to use different random number generators…

---

<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:** [April 11, 2019, 3:12pm UTC](https://discourse.julialang.org/t/randomly-generate-a-sequence-of-numbers-one-by-one-using-the-rand-function-in-distributions-module/23022/7 "2019-04-11T15:12:56Z")

</div>

I don’t really understand where is your problem… what about creating a second rng outside of the loop, and make the `rand` calls as needed within the loop?

---

<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:** [April 11, 2019, 4:00pm UTC](https://discourse.julialang.org/t/randomly-generate-a-sequence-of-numbers-one-by-one-using-the-rand-function-in-distributions-module/23022/8 "2019-04-11T16:00:15Z")

</div>

```julia
julia> using Random

julia> rng = MersenneTwister(4)

julia> using Distributions

julia> for i = 1:10
           r = rand(rng,Categorical([0.4,0.6]))
           println(r)
       end
2
2
2
2
2
2
1
2
2
2

```

It’s not what is expected.

---

<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:** [April 11, 2019, 4:03pm UTC](https://discourse.julialang.org/t/randomly-generate-a-sequence-of-numbers-one-by-one-using-the-rand-function-in-distributions-module/23022/9 "2019-04-11T16:03:38Z")

</div>

> [@bsnyh](#):
>
> It’s not what is expected.

What do you expect?

---

<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:** [April 11, 2019, 4:07pm UTC](https://discourse.julialang.org/t/randomly-generate-a-sequence-of-numbers-one-by-one-using-the-rand-function-in-distributions-module/23022/10 "2019-04-11T16:07:35Z")

</div>

oh. You’re right. I thought the output above is all 2’s, and did not notice that there is a 1 hidden inside.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [April 11, 2019, 4:13pm UTC](https://discourse.julialang.org/t/randomly-generate-a-sequence-of-numbers-one-by-one-using-the-rand-function-in-distributions-module/23022/11 "2019-04-11T16:13:53Z")

</div>

I’d guess that you might be better off creating that `Categorical` distribution object outside of the loop.

---

<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:** [April 11, 2019, 4:18pm UTC](https://discourse.julialang.org/t/randomly-generate-a-sequence-of-numbers-one-by-one-using-the-rand-function-in-distributions-module/23022/12 "2019-04-11T16:18:36Z")

</div>

why?

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [April 11, 2019, 4:21pm UTC](https://discourse.julialang.org/t/randomly-generate-a-sequence-of-numbers-one-by-one-using-the-rand-function-in-distributions-module/23022/13 "2019-04-11T16:21:10Z")

</div>

Because it’s the same every time and it’s pretty hard for the compiler to prove that it doesn’t need to create a new array and object referring to that array on every iteration.

---

<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:** [April 11, 2019, 4:22pm UTC](https://discourse.julialang.org/t/randomly-generate-a-sequence-of-numbers-one-by-one-using-the-rand-function-in-distributions-module/23022/14 "2019-04-11T16:22:12Z")

</div>

```julia
julia> using Random
julia> using Distributions
julia> rng1 = MersenneTwister(1)
julia> rng2 = MersenneTwister(2)
julia> for i = 1:10
           r = rand(rng1, Categorical([0.4,0.6]))
           println("r: ", r)
           t = rand(rng2, Exponential(1/200))
           println("t: ", t)
       end
r: 1
t: 0.0030177027481547076
r: 1
t: 0.009348155469112253
r: 1
t: 0.004486075863176537
r: 1
t: 0.007756887782850566
r: 2
t: 0.007259221922800534
r: 1
t: 0.0038109894089538372
r: 2
t: 0.0015894111269976982
r: 2
t: 0.004725440286443651
r: 1
t: 0.006614692669167275
r: 2
t: 0.0026035963682239554

julia> for i = 1:10
           r = rand(MersenneTwister(1), Categorical([0.4,0.6]))
           t = rand(MersenneTwister(4), Exponential(1/200))
           println("r: ", r)
           println("t: ", t)
       end
r: 1
t: 0.011176055504085384
r: 1
t: 0.011176055504085384
r: 1
t: 0.011176055504085384
r: 1
t: 0.011176055504085384
r: 1
t: 0.011176055504085384
r: 1
t: 0.011176055504085384
r: 1
t: 0.011176055504085384
r: 1
t: 0.011176055504085384
r: 1
t: 0.011176055504085384
r: 1
t: 0.011176055504085384

```

The above comparison explains my confusing point. I know how it works now. I guess calling the `MersenneTwister()` function is like re-setting the global random number generator. Or can anyone explain it more clearly?

---

<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:** [April 11, 2019, 4:33pm UTC](https://discourse.julialang.org/t/randomly-generate-a-sequence-of-numbers-one-by-one-using-the-rand-function-in-distributions-module/23022/15 "2019-04-11T16:33:16Z")

</div>

Each time you call `MersenneTwister(4)` “is like” re-seeding the global RNG, so doing that in the loop will produce invariably the same output. Moreover, it’s very wasteful to create so many new RNGs. By the way, do you really need to create two RNGs for the two distributions? In your example above at least it’s unnecessary.

---

<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:** [April 11, 2019, 4:48pm UTC](https://discourse.julialang.org/t/randomly-generate-a-sequence-of-numbers-one-by-one-using-the-rand-function-in-distributions-module/23022/16 "2019-04-11T16:48:05Z")

</div>

to make sure they are strictly not related at all…

---

<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:** [April 11, 2019, 9:52pm UTC](https://discourse.julialang.org/t/randomly-generate-a-sequence-of-numbers-one-by-one-using-the-rand-function-in-distributions-module/23022/18 "2019-04-11T21:52:14Z")

</div>

Are you sure that makes them _more_ independent? I’m no expert on this, but it sounds to me like the type of ‘clever’ overcomplication that could end up making them _less_ independent. You should definitely double check your assumption.

Also, why are you redefining the distributions (to the same values!) on each loop iteration?

---

<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:** [April 12, 2019, 6:24am UTC](https://discourse.julialang.org/t/randomly-generate-a-sequence-of-numbers-one-by-one-using-the-rand-function-in-distributions-module/23022/19 "2019-04-12T06:24:18Z")

</div>

As a rule of thumb, create independent RNGs for multithreaded code, otherwise use the same one sequentially.

The most important exception to this is CI, but `@testset` [Does the Right Thing™](https://docs.julialang.org/en/v1/stdlib/Test/#Test.@testset) so you don’t need to worry about that either.
