Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Random.seed! not reproducible #2290

Open
jbrea opened this issue Sep 20, 2022 · 5 comments
Open

Random.seed! not reproducible #2290

jbrea opened this issue Sep 20, 2022 · 5 comments
Assignees
Labels
reactivity The Pluto programming paradigm

Comments

@jbrea
Copy link
Contributor

jbrea commented Sep 20, 2022

22 09 20T09:22:18
I expected the two random numbers to be the same, similarly as to what we see in the repl when we run

julia> using Random

julia> Random.seed!(123)
TaskLocalRNG()

julia> rand()
0.521213795535383

julia> Random.seed!(123)
TaskLocalRNG()

julia> rand()
0.521213795535383

notebook.tar.gz

@jbrea
Copy link
Contributor Author

jbrea commented Sep 20, 2022

ps. This happens on Julia 1.7 and 1.8. On Julia 1.6 Pluto behaves like the REPL.

@Pangoraw
Copy link
Collaborator

The difference with 1.6 is caused by this change: JuliaLang/julia#40546. Each eval is made in a different task in Pluto, thus the RNG is not inherited.

@fonsp
Copy link
Owner

fonsp commented Sep 21, 2022

@Pangoraw thanks! Does that mean that this is the intended result? Should/could we change it?

@fonsp fonsp added the reactivity The Pluto programming paradigm label Oct 12, 2022
@fonsp
Copy link
Owner

fonsp commented Oct 17, 2022

I would say that this is intentional, and the way to get reproducible RNG across cells is to create an RNG object and pass it to other cells. @Pangoraw WDYT?

@Pangoraw
Copy link
Collaborator

With the task based rng passing it explicitely does not work since the task local state is not preserved across cells:

rng = Random.seed!(42)

randn(rng, 10)

One workaround for reproducible randomness is to use the previous Random.MersenneTwister generator:

rng = Random.MersenneTwister(42)

randn(rng, 10)

To fix inside pluto, we need to use a persistent task with an loop to execute cells like in IJulia for example:
https://github.com/JuliaLang/IJulia.jl/blob/2748004d9a7ad520a31397566885970072a6fd05/src/eventloop.jl#L4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
reactivity The Pluto programming paradigm
Projects
None yet
Development

No branches or pull requests

3 participants