How do I deal with Random Number generation when multithreading

You can use randjump to get independent streams (by default randjump jumps ahead 10^20 steps)

EDITED

num_paths = 10
num_draws_per_path = 1000

# set random seed and output with results for later reproducibility
rng_seed = Base.Random.make_seed()

# to reproduce results, set explicit seed from previous run
# rng_seed = UInt32[0xbd8e7dc4,0x0d00f383,0xdccee4c9,0x553afb99]

rng = MersenneTwister(rng_seed)
# RNG per thread
rngs = randjump(rng, Threads.nthreads());
rand_nums = Vector{Vector{Float64}}(num_paths);

Threads.@threads for p = 1:num_paths
    rand_nums[p] = randn(rngs[Threads.threadid()], num_draws_per_path);
end

Reproducibility using a separate RNG per thread relies on loop iterations being allocated to threads deterministically. I think this is true with current implementation. However changing the number of threads available will break reproducibility.

In that case, a safer option might be to use a separate RNG per path:

# RNG per path
rngs = randjump(rng, num_paths);
rand_nums = Vector{Vector{Float64}}(num_paths);

Threads.@threads for p = 1:num_paths
    rand_nums[p] = randn(rngs[p], num_draws_per_path);
end
2 Likes