Question

I'm currently running a program which is multithreaded via the snow package (using clusterApplyLB) and in the code I generate a random number, but it seems the threads are all using the same seed to generate this number so they give the same result.

Is there a way to have the different threads start with different seeds?

I can't just generate a random seed for each thread, because this would have the same problem, and I'm also not keen on the idea of having a new index argument for the seed in the function called by each thread (i.e. the function being called assigns 1:32 as seeds for each thread as this isn't really "random.")

Was it helpful?

Solution

The clusterSetupRNG function in the snow package will set a different random number stream on each of the members of a cluster. The clusterSetRNGStream function in the parallel package does the same thing. Just call one of those functions before calling clusterApplyLB and everything should work fine.

You may also want to look at the harvestr package for another way to manage the multiple random number streams (in a way that focuses on making the whole process reproducible).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top