# Problem with random generator

**URL:** https://discourse.julialang.org/t/problem-with-random-generator/37311
**Category:** General Usage
**Created:** [April 10, 2020, 3:06am UTC](https://discourse.julialang.org/t/problem-with-random-generator/37311 "2020-04-10T03:06:14Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![sergevic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergevic/32/9958_2.png) [@sergevic](https://discourse.julialang.org/u/sergevic)
#### Post date: [April 10, 2020, 3:06am UTC](https://discourse.julialang.org/t/problem-with-random-generator/37311/1 "2020-04-10T03:06:14Z")

</div>

I’m completely puzzled with a problem concerning my code:

```julia
using LinearAlgebra
using DeterminantalPointProcesses

L=Symmetric(rand(100,100))
variables=collect(1:100)
dpp=DPP(eigen(L))
M0=rand(dpp,1)
function proposal(Mt::Array{Int64,1})
    compl=setdiff(variables,Mt)
    Lcompl=L[compl,compl]
    dpp_compl=DPP(eigen(Lcompl))
    M1=compl[rand(dpp_compl,1)[1]]
    return M1
end

```

When I run `map(x->proposal(M0[1]),1:3)` I get

```julia
3-element Array{Array{Int64,1},1}:
 [2, 3, 4, 5, 6, 8, 10, 11, 12, 13 … 84, 87, 88, 89, 93, 94, 97, 98, 99, 100]
 [2, 3, 4, 5, 6, 8, 10, 11, 12, 13 … 84, 87, 88, 89, 93, 94, 97, 98, 99, 100]
 [2, 3, 4, 5, 6, 8, 10, 11, 12, 13 … 84, 87, 88, 89, 93, 94, 97, 98, 99, 100]

```

but I’m supposed to obtain 3 different arrays, because of the random command `rand(dpp_compl,1)[1]` .  
When I try to run

```julia
Mt=M0
compl=setdiff(variables,Mt[1])
Lcompl=L[compl,compl]
dpp_compl=DPP(eigen(Lcompl))
compl[rand(dpp_compl,1)[1]]
compl[rand(dpp_compl,1)[1]]
compl[rand(dpp_compl,1)[1]]

```

I obtain 3 different arrays and that’s what I should obtain. I’ completely puzzled because the first code should produce different arrays just like the second code! Has anyone an idea of what’s wrong??

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [April 10, 2020, 4:44am UTC](https://discourse.julialang.org/t/problem-with-random-generator/37311/2 "2020-04-10T04:44:25Z")

</div>

It is returning a `Vector` of `Vector`s, i.e. each component of the result is one of the `Vector`s that you wanted.

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [April 10, 2020, 4:48am UTC](https://discourse.julialang.org/t/problem-with-random-generator/37311/3 "2020-04-10T04:48:06Z")

</div>

I think the complaint is that the three entries are the same.

---

<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 10, 2020, 4:55am UTC](https://discourse.julialang.org/t/problem-with-random-generator/37311/4 "2020-04-10T04:55:46Z")

</div>

I didn’t run the code, but it looks likely to come from the fact that in your `map`command, the function returns a constant (i.e. doesn’t depend on its argument `x`.

---

<div class="post-metadata">

### Author: ![WschW](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wschw/32/6575_2.png) [@WschW](https://discourse.julialang.org/u/WschW)
#### Post date: [April 10, 2020, 5:19am UTC](https://discourse.julialang.org/t/problem-with-random-generator/37311/5 "2020-04-10T05:19:12Z")

</div>

Each time you call `DPP` the `DeterminantalPointProcesses` it creates contains a `MersenneTwister` created with the same seed.

> <https://github.com/alshedivat/DeterminantalPointProcesses.jl/blob/80ffff8c9e33b1cfd43fcfe7379f8ffd80ba7240/src/types.jl#L12>

Since the `rand` method provided by the package uses that `MersenneTwister` you will always get the same sequence if you create the `DeterminantalPointProcesses` without setting the seed to a different number each time you create one.

---

<div class="post-metadata">

### Author: ![sergevic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergevic/32/9958_2.png) [@sergevic](https://discourse.julialang.org/u/sergevic)
#### Post date: [April 10, 2020, 5:00pm UTC](https://discourse.julialang.org/t/problem-with-random-generator/37311/6 "2020-04-10T17:00:44Z")

</div>

The problem is that the three vectors are the same when they don’t have to be! 🙂

---

<div class="post-metadata">

### Author: ![sergevic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergevic/32/9958_2.png) [@sergevic](https://discourse.julialang.org/u/sergevic)
#### Post date: [April 10, 2020, 5:01pm UTC](https://discourse.julialang.org/t/problem-with-random-generator/37311/7 "2020-04-10T17:01:03Z")

</div>

Yes exactly!

---

<div class="post-metadata">

### Author: ![sergevic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergevic/32/9958_2.png) [@sergevic](https://discourse.julialang.org/u/sergevic)
#### Post date: [April 10, 2020, 5:01pm UTC](https://discourse.julialang.org/t/problem-with-random-generator/37311/8 "2020-04-10T17:01:56Z")

</div>

The argument `x` is just here to settle the numer of times I repeat the function `proposal`.

---

<div class="post-metadata">

### Author: ![sergevic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergevic/32/9958_2.png) [@sergevic](https://discourse.julialang.org/u/sergevic)
#### Post date: [April 10, 2020, 5:04pm UTC](https://discourse.julialang.org/t/problem-with-random-generator/37311/9 "2020-04-10T17:04:24Z")

</div>

Ahhhhhhh yes! Didn’t see that the seed was inside the `rand` command, since usually I set a `seed` myself outside! That should do the trick, then!
