Stream: helpdesk (published)

Topic: parallel seeds for Distributions.jl


view this post on Zulip Rein Zustand (May 27 2021 at 12:41):

Currently, if I want to parallelize my independent computations (e.g. 10 monte carlo paths), on each subprocess, I initialize an RNG, rng = Random.MersenneTwister(process_number), and then generate a random number using e.g. randn(rng). But I can't seem able to feed this rng object if I were to use a distribution object from Distributions.jl.

import Distributions
var = rand(Distributions.Uniform(1.0, 20.0))

Is there a way to feed in the rng object into the code above?

view this post on Zulip Fredrik Ekre (May 27 2021 at 12:46):

You can pass the rng as the first argument to rand as usual, I think?

view this post on Zulip Fredrik Ekre (May 27 2021 at 12:47):

julia> using Distributions, Random

julia> rand(MersenneTwister(1234), Uniform(1.0, 20.0))
12.226048134648494

julia> rand(MersenneTwister(1234), Uniform(1.0, 20.0))
12.226048134648494

view this post on Zulip Rein Zustand (May 27 2021 at 12:49):

That solves it, thank you very much!


Last updated: Nov 06 2024 at 04:40 UTC