Skip to content

unpredictable behavior of Random.seed! on workers using Distributed #51225

@vancleve

Description

@vancleve

I'm having trouble setting the seed on worker tasks using Distributed. If I set the seed and then get a random number in one remote call, the result is correct. If I set the seed and then get the random number in two remote calls, the random number is different each time.

MWE

using Distributed

addprocs(1)
@everywhere using Random

# works on main process
Random.seed!(1234); rand() 
fetch(@spawnat 1 (Random.seed!(1234); rand()))

# works in one call
fetch(@spawnat 2 (Random.seed!(1234); rand()))

# doesn't work in two calls
fetch(@spawnat 2 Random.seed!(1234)); fetch(@spawnat 2 rand())
fetch(@spawnat 2 Random.seed!(1234)); fetch(@spawnat 2 rand())

Output:

0.32597672886359486
0.32597672886359486

0.32597672886359486

0.23226466961312875
0.11712298602126525
versioninfo()
Julia Version 1.9.3
Commit bed2cd540a1 (2023-08-24 14:43 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: macOS (arm64-apple-darwin22.4.0)
  CPU: 12 × Apple M2 Max
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-14.0.6 (ORCJIT, apple-m1)
  Threads: 1 on 8 virtual cores
Environment:
  JULIA_EDITOR = code
  JULIA_NUM_THREADS = 

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsThis change adds or pertains to documentationrandomnessRandom number generation and the Random stdlib

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions