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! does not yield reproducible numbers #49522

Closed
RossBoylan opened this issue Apr 26, 2023 · 4 comments
Closed

Random.seed! does not yield reproducible numbers #49522

RossBoylan opened this issue Apr 26, 2023 · 4 comments

Comments

@RossBoylan
Copy link

Summary

The random numbers returned after setting the seed in the primary thread vary with the number of threads/tasks in use.

Desired Solution

Ideally, there would be no such dependence.

More realistically, this behavior should be readily apparent in the documentation.

Example

using Random

Random.seed!(739587835)

function worker()
    sleep(0.2)
end

# launch workers
tasks = [Threads.@spawn worker() for i in 1:Threads.nthreads()]
println(Random.randn(10))

Results

-t varies the number of threads, and as they vary the random numbers produced change. Setting the seed does not assure reproducibility.

PS C:\Users\rdboylan\Documents\BP\MSEP> julia --project -t16 -- src\test6.jl
[-0.6096911806529702, -0.4542257247591477, 0.16528763905031515, -0.12062756001589506, 0.8968783357334372, 0.5060655128978275, -0.34218046669006463, -0.3526521145673422, 0.4518655487713241, -1.1686014861086333]
PS C:\Users\rdboylan\Documents\BP\MSEP> julia --project  -- src\test6.jl
[0.40401615236372684, -2.4530141213201877, 0.7609440796042747, 0.4931999850247413, -0.4117305272372651, 0.8822169514594856, -0.07503118310039182, 0.22225124841559304, 0.5964174113780768, 0.288255723358961]
PS C:\Users\rdboylan\Documents\BP\MSEP> julia --project -t1  -- src\test6.jl
[0.40401615236372684, -2.4530141213201877, 0.7609440796042747, 0.4931999850247413, -0.4117305272372651, 0.8822169514594856, -0.07503118310039182, 0.22225124841559304, 0.5964174113780768, 0.288255723358961]
PS C:\Users\rdboylan\Documents\BP\MSEP> julia --project -t2  -- src\test6.jl
[-0.4117305272372651, 0.8822169514594856, -0.07503118310039182, 0.22225124841559304, 0.12712738849715016, -0.5346188720010416, -1.8661491365928784, 0.466611932906786, -1.0209655744896966, -0.24667618682784703]
PS C:\Users\rdboylan\Documents\BP\MSEP> julia --project -t3  -- src\test6.jl
[1.3291320383393166, -0.5346188720010416, -1.8661491365928784, 0.46661193

Varying the number of threads without spawning any tasks does not give this behavior. The random numbers are the same regardless of the number of threads when there are no tasks.

Comment

I guess this is the result of the interaction of tasks and the RNG. In particular, TaskLocalRNG says "task creation is an event that changes the parent's RNG state". Note that the test program makes no explicit reference to TaskLocalRNG.

If this behavior is documented at https://docs.julialang.org/en/v1/stdlib/Random, I don't see it. That documentation refers to GLOBAL_RNG as a default argument without defining it. It does say the generator has task specific state; it is unclear how the main thread figures into that.

Environment

julia> versioninfo()
Julia Version 1.8.5
Commit 17cfb8e (2023-01-08 06:45 UTC)
Platform Info:
OS: Windows (x86_64-w64-mingw32)
CPU: 20 × Intel(R) Xeon(R) CPU E5-2690 v4 @ 2.60GHz
WORD_SIZE: 64
LIBM: libopenlibm
LLVM: libLLVM-13.0.1 (ORCJIT, broadwell)
Threads: 16 on 20 virtual cores
Environment:
JULIA_EDITOR = code
JULIA_NUM_THREADS =

Installed by download from julialang.org.

@jmkuhn
Copy link
Contributor

jmkuhn commented Apr 26, 2023

See #49064

@JeffBezanson
Copy link
Member

Fixed by #49110.

@RossBoylan
Copy link
Author

Fantastic! Do I read the fix correctly: it's in julia 1.9?

@oscardssmith
Copy link
Member

No. This will be in 1.10

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

No branches or pull requests

4 participants