[R] (Parallel) Random number seed question...

Torsten Hothorn Torsten.Hothorn at stat.uni-muenchen.de
Mon Nov 16 18:24:54 CET 2009


On Mon, 16 Nov 2009, Blair Christian wrote:

> Hi All,
>
> I have k identical parallel pieces of code running, each using n.rand
> random numbers.  I would like to use the same RNG (for now), and set
> the seeds so that I can guarantee that there are no overlaps in the
> random numbers sampled by the k pieces of code.  Another side goal is
> to have reproducibility of my results.  In the past I have used C with
> SPRNG for this task, but I'm hoping that there is an easy way to do
> this in R with poor man's parallelization (eg running multiple Rs on
> multiple processors without the overhead of setting up any mpi or
> using snow(fall)).  It is not clear from the documentation if set.seed
> arguments are sequential or not for a given RNG, eg if set.seed(1) on
> processor 1, set.seed(1+n.rand) on processor 2, set.seed(1+2*n.rand)
> on processor 3, etc for the default RNG "Mersenne-Twister".  An easy
> approach would be to simply write a script to generate n.rand numbers,
> records the .Random.seed and proceeds in that manner- inelegant, but
> effective.  My question here is "Is there a better way?"
>
>
> (mvtnorm part directed to Torsten Hothorn)
> To further clarify, it seems there is a different RNG for normal
> (rnorm) than for everything else? (eg RNGKind( ..,
> normal.kind="Inversion"); further, does anybody know if mvtnorm uses
> this generator?
>

mvtnorm is based on FORTRAN code which uses unif_rand() from the C API:

void F77_SUB(rndstart)(void) { GetRNGstate(); }
void F77_SUB(rndend)(void) { PutRNGstate(); }
double F77_SUB(unifrnd)(void) { return unif_rand(); }

Torsten

>
> Further, some RNGs seem to be based on the archictecture (eg the
> Knuth-TAOCP-2002 for example)- is the period really related to 2^32,
> or is it dependent the architecture, 2^64 for 64 bit R and 2^32 for 32
> bit R?
>
>
> I noticed there are several packages related to RNG- please direct me
> to a vignette/R news article/previous post if this has been covered ad
> nauseum.  I have skimmed vignettes/docs for rsprng package, RNG doc in
> base, setRNG package, mvtnorm package vignette  (Or am I setting
> myself up to write a current RNG doc?)
>
>
> (directed to Gregory Warnes)
> I found a presentation by Gregory Warnes from 1999 addressing these
> same questions (and uses a "collings generator" in some C code).
> http://www.r-project.org/conferences/DSC-1999/slides/warnes.ps.gz
> Have you turned to the snowfall related parallel implementations, did
> your Collings generator work well, or have you discovered another
> trick you might like to share?
>
> Thank you all for your time and excellent contributions to the open
> source community,
> Blair
>
>


More information about the R-help mailing list