If your goal is that each call to rand(), regardless of process,
sequentially pulls a number from the RNG stream, then perhaps
you just need to create a MersenneTwister([seed]) object.mystream = MersenneTwister(1)## Parallel execution
srand(1)
parfor = @parallel (vcat) for i=1:4
rand(mystream)
end
Yeah, sorry - I suspected there might be something I was missing. According to the wikipedia page about the MT one of its disadvantages is just that:
It can take a long time to turn a non-random initial state—particularly an initial state with many zeros—into output that passes randomness tests. A consequence of this is that two instances of the generator, started with initial states that are almost the same, will output nearly the same sequence for many iterations before eventually diverging.
So you should probably not do this without significant warm-up, or in some other way make sure that the seeds are different enough. If you don’t need randoms very often, you’re probably better off with James’ approach.
// T
One thing we can do is use SFMT Jump. It's dependencies are not so straightforward to build, and I am not sure how well supported this is, but it may be the best way to get parallel streams.
Since I also agree that Monte Carlo in parallel seems like it is unavoidably "distributed", you should require each process to work on non-overlapping substreams of the same stream. I wonder (mainly for my own purposes) if this pmap() implementation is all that "safe", in the meantime, before a SFMT Jump is implemented.That is, if you end up generating more than one rand() in a process, you are not guaranteed that just by setting the seed will give you the same results in each time you run it.In the meantime, might it be preferable to just generate all the rand()s you are ever going to need, serially, ahead of time, put them in a distributed array, and have your parallel processes work on their separate pieces of the distributed array?