[Rcpp-devel] mixing R's and C++'s RNGs and distributions
Ramon Diaz-Uriarte
rdiaz02 at gmail.com
Fri Jun 19 09:39:38 CEST 2015
Dear Matt,
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.
Thanks a lot! Yes, certainly interesting, a lot. I felt embarrassed by my
way of seeding the PRNG from R.
I had seen randutils.hpp mentioned before, but had never payed any
attention. After your links below (and thanks for the archive URLs ---last
night those were the only ones I could access) I think I'll be
incorporating it into my work. Not only because of the seeding, but also
because of the additional facilities (choose, pick, etc).
> The discussion threads (involving the author) may also be worth a look.
Yes, they were. Thanks!
> (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/
>
I wasn't aware of this.
>> 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
Yes; I figured that if what I understood were similar approaches (mixing
R's and C++s' PRNG and seeding from R) worked in more complex scenarios,
they should work on mine.
> 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)
Nice; I'll remember this for if/when I need them in a parallel computing
context.
Thanks,
R.
>
> 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