jeudi 2 septembre 2021

Setting seeds in multi-threading loop in Julia

I want to generate random numbers in Julia using multi-threading. I am using the Threads.@threads macro to accomplish it. However, I struggle fixing the number of seeds to obtain the same result every time I run the code. Here is my trial:

Random.seed!(1234)
a = [Float64[] for _ in 1:10]

Threads.@threads for i = 1:10
    push!(a[Threads.threadid()],rand())
end

sum(reduce(vcat, a))

The script above delivers different results every time I run it. By contrast, I get the same results if I use a plain for loop:

Random.seed!(12445)
b = []

for i = 1:10
    push!(b,rand())
end

sum(b)

I have the impression that the solution to this issue must be easy. Still, I couldn't find it. Any help is much appreciated.

Thank you.




Aucun commentaire:

Enregistrer un commentaire