Date: Thu, 4 Jun 2015 19:47:27 +0200
From: Guillaume Chapron <carnivorescience at gmail.com>
To: r-package-devel at r-project.org
Subject: [R-pkg-devel] Managing RNG in C code
Message-ID: <A1D77159-A342-40AE-8ED3-529019DCA70E at gmail.com>
Content-Type: text/plain; charset=us-ascii
Hello,
I am working on a package where I am passing some computations from R to C
and back. The C code runs many stochastic trajectories of a population
model (with the RNG from R through multiple calls to GetRNGstate and
PutRNGstate). I would like that each trajectory has its own seed.
My problem is that the guidelines to develop R packages write in the
section '6.3 Random number generation' that "The random number generator is
private to R; there is no way to select the kind of RNG or set the seed
except by evaluating calls to the R functions."
The only way I see to get around this is to set the seed in R, run 1
trajectory in C, get results back in R, increment the seed in R, run a 2nd
trajectory in C with this new seed, add the new results to the previous one
in R, and repeat this e.g. 1000 times. Does this sound fine or should I be
concerned that calling the C library so many times may slow down the
computation (even if the library is only loaded 1 time at the beginning),
e.g. is there a substantial overhead in interfacing from R to C and back?
Alternatively, is there a hack to get access by force to the private RNG
in R from C and set its seed? This way, I could call the C code only 1 time
and run the 1000 trajectories within C, updating the seed in R for each new
trajectory (this is what I did when I previously used the GSL).
Thanks for any advice on this!
Guillaume