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

Ramon Diaz-Uriarte rdiaz02 at gmail.com
Sat Jun 20 21:13:04 CEST 2015


As a follow up to the thread below, using randutils has turned out to be
very simple (and provides some additional nice features, such as pick,
etc). Directly using it in an R package makes R CMD check complain about
the usage of the Exit function (only its address is used, not the function
itself). That can be solved by changing the line

auto exit_func = hash(&_Exit);

by, say

auto getenv_func = hash(&getenv);

and making the corresponding change a little bit further below.


Best,

R.




On Fri, 19-06-2015, at 00:13, Matt D. <matdzb at gmail.com> wrote:
> 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
>
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

-- 
Ramon Diaz-Uriarte
Department of Biochemistry, Lab B-25
Facultad de Medicina
Universidad Autónoma de Madrid 
Arzobispo Morcillo, 4
28029 Madrid
Spain

Phone: +34-91-497-2412

Email: rdiaz02 at gmail.com
       ramon.diaz at iib.uam.es

http://ligarto.org/rdiaz


More information about the Rcpp-devel mailing list