[Rcpp-devel] mixing R's and C++'s RNGs and distributions

Matt D. matdzb at gmail.com
Fri Jun 19 00:13:03 CEST 2015


On 6/18/2015 14:34, Ramon Diaz-Uriarte wrote:
> Dear All,
>
> Sometimes I use both R's and C++11's RNGs and distributions in the same
> code base (I am not using OpenMP or similar).  Although this might not be
> very elegant, I find it convenient (use C++'s or R's, depending on which
> one fits my problem better ---in particular, many distributions are not
> readily available from C++). In C++ I tend to use std::mt19937, often with
> a seed generated in R as
>
> seed <- as.integer(round(runif(1, min = 0, max = 2^16)))
>
> and passed to the C++ code.
Seeding the Mersenne Twister PRNG can a bit subtle: I've ran across a 
series of posts a while ago that have brought up a couple of issues I 
haven't considered before -- perhaps you'll also find them of interest. 
The discussion threads (involving the author) may also be worth a look.
(There seems to be a connectivity issue with "www.pcg-random.org" at the 
moment, so also providing the "archive.is" URLs to be on the safe side.)

http://www.pcg-random.org/posts/cpp-seeding-surprises.html
// 
http://archive.is/http://www.pcg-random.org/posts/cpp-seeding-surprises.html
Discussions:
http://www.reddit.com/r/cpp/comments/32u4m7/the_behavior_of_rng_seeding_in_c_may_surprise/
http://www.reddit.com/r/programming/comments/32uo1p/c_seeding_surprises/

https://www.pcg-random.org/posts/developing-a-seed_seq-alternative.html
// 
http://archive.is/http://www.pcg-random.org/posts/developing-a-seed_seq-alternative.html

http://www.pcg-random.org/posts/simple-portable-cpp-seed-entropy.html
http://archive.is/http://www.pcg-random.org/posts/simple-portable-cpp-seed-entropy.html

http://www.pcg-random.org/posts/ease-of-use-without-loss-of-power.html
// 
http://archive.is/http://www.pcg-random.org/posts/ease-of-use-without-loss-of-power.html

In particular, `randutils::auto_seed_128` and `randutils::auto_seed_256` 
from the last post may be a choice worth consideration:
https://gist.github.com/imneme/540829265469e673d045#file-randutils-hpp

Discussion: 
http://www.reddit.com/r/cpp/comments/34yqxa/announcing_randutils_a_single_portable/

One other thing useful to know (in the context of, say, reproducibility) 
is that (in contrast with the PRNGs themselves) the "distributions' 
algorithms are not mandated, so implementations can vary":
http://www.reddit.com/r/cpp/comments/30w7cs/inconsistency_in_c_random/

> It is my understanding that similar ideas (seeding C++'s RNG from R and
> combining C++ with R's RNG) have been used before in much more complex
> settings (e.g.,
> http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2014-April/007510.html
> and
> http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2014-April/007512.html),
> but I wonder if there are problems I cannot think of. A silly example
> follows below.
The links seems to be referring to a parallel computing context. Given 
that, I'd actually consider using Random123:
http://www.thesalmons.org/john/random123/
https://github.com/DEShawResearch/Random123-Boost // a nice, brief 
overview of the advantages in the parallel computing context (but also 
potentially applicable elsewhere)

Best,

Matt



More information about the Rcpp-devel mailing list