[R-pkg-devel] R-extension requirement about third-party random number generators (RNG)

Jeff Newmiller jdnewm|| @end|ng |rom dcn@d@v|@@c@@u@
Fri Sep 27 17:25:12 CEST 2024


You mention that you did not like putting the rstream in the user API but there is nothing forcing you to have the user call your cpp function directly... there are lots of packages that use R glue functions as the API.

I have not dug into Dirk's code, but having the native R seed management is IMO a major plus for following WRE literally rather than merely echoing similar behavior in a specific run from the same starting point.

On September 27, 2024 7:58:19 AM PDT, John Clarke <john.bradley.clarke using gmail.com> wrote:
>Hi Dirk,
>
>Thanks very much for your thoughtful reply. This is a relief for me as my
>'solution' for replacing the existing RNG in my C++ was convoluted and I
>couldn't see the advantage in doing so. Thank you for the search tip --
>that is really helpful and I did not know that there was this kind of
>mirroring. Thanks also for the mention of the rcpp-devel -- I think I will
>use that instead next time.
>
>One point of contention though -- I think the Writing R-Extensions page
>could be more explicit in this regard and perhaps suggest times/examples
>when it is OK to include an existing (3rd party) RNG and when it is not.
>
>I also think that it would be helpful if R offered a way to instantiate
>multiple instances of its internal RNG manager out of the box -- the RNG
>state management strategy appears almost 'magical' to me especially
>inside RCPP. It is possible, I just don't understand how to use it.
>
>Best,
>
>-John
>
>On Fri, Sep 27, 2024 at 4:19 PM Dirk Eddelbuettel <edd using debian.org> wrote:
>
>>
>> Hi John,
>>
>> I think you are reading the text too literally. The intent of WRE is to
>> ensure that standard use of a RNG in an extension package uses the RNGs
>> that
>> come with R (which includes an updated mersenne twister algorithm) so that
>> users are not "surprised".  It explicitly mentions the problem of multiple
>> seeding.  An example of how this can be done is in RcppArmadillo: a long
>> time
>> ago we worked out a scheme were in the R use case the RNG is 'dropped in'
>> so
>> that Armadillo code uses randu(5) you get what runif(5) in R would give you
>> (given the same seed from R).
>>
>> On the other hand, when you know what you do and properly (locally)
>> instantiate another PRNG for local use you can. For quick checks I often
>> use
>> a query at github in the 'cran' organisation mirroring the CRAN repos. For
>> example the following shows where std::mt19937 is used in C++ to deploy the
>> Mersenne Twister. So as you can see, when done carefully it is in fact
>> allowed.
>>
>>    https://github.com/search?q=org%3Acran%20mt19937&type=code
>>
>> Hope this helps, and allow me to mention that there is also the rcpp-devel
>> for more Rcpp-specific questions.
>>
>> Cheers, Dirk
>>
>>
>> --
>> dirk.eddelbuettel.com | @eddelbuettel | edd using debian.org
>>
>
>	[[alternative HTML version deleted]]
>
>______________________________________________
>R-package-devel using r-project.org mailing list
>https://stat.ethz.ch/mailman/listinfo/r-package-devel

-- 
Sent from my phone. Please excuse my brevity.



More information about the R-package-devel mailing list