[R-pkg-devel] Managing RNG in C code
Charles Geyer
charlie at stat.umn.edu
Fri Jun 5 22:55:41 CEST 2015
The answer is do not try to manage the RNG from C. Just do
GetRNGstate();
then generate a lot of random variates and then
PutRNGstate();
just before leaving. There is only one call to each in one call of C from
R.
This is TRT (the Right Thing). Anything else is TWT (the Wrong Thing).
Starting an RNG a whole bunch of times with lots of different seeds doesn't
do random number generation. You want one continuous stream. Only then
does the RNG have the properties claimed for it.
Moreover, you want your code to follow the R way (the tao of R). Your use
of RNG should be just like every other R function.
The user should be able to save .Random.seed and restore it and get the
same results with another run. So the user is doing reproducible
research. The user shouild be able to use RNGkind to change the random
number generator. If you muck about with this you get in the way of the
users. Your code is less useful not more.
In my packages that use random number generation (especially mcmc), this is
the policy I follow. The only thing I do with the random number generator
is save .Random.seed before going from R to C and store the saved value in
the returned object so the user does not have to.
As with many problems, the right answer is don't do that!
Message: 1
> 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
>
--
Charles Geyer
Professor, School of Statistics
University of Minnesota
charlie at stat.umn.edu
[[alternative HTML version deleted]]
More information about the R-package-devel
mailing list