[Rcpp-devel] Setting seed using Rcpp

Hmamouche Youssef hmamoucheyussef at gmail.com
Wed Feb 7 17:20:08 CET 2018


Thank you all for these answers.

 If you try "rcpp random number generation" in a search engine,
 the first line will point to

 http://gallery.rcpp.org/articles/random-number-generation/

 which probably has your answer.

 Best,
 Serguei

Yes of course i tried http://gallery.rcpp.org/articl
es/random-number-generation/, which helps to replace the rand function, and
effectively, i found the set.seed () (from R), just i didn't know how t use
it in C++.
-----------------------------------------------------------------
-----------------

In short, no.

Slightly longer: All Rcpp code is set up to be used _from R_ so by design
you
are always in R before you call code containing Rcpp.  So just call the R
function set.seed(...) as we work hard to make sure the state is properly
set
and passed before and after.

Even longer: srand() is always wrong and bad (long literature on that ...)
and has nothing to do with R's RNG.  You are entirely free to define and
implement your own RNG (see my RcppZiggurrat package revisiting Marsaglia's
Ziggurat RNG) or call others (C++11 has some) or ...  But you probably want
to use R's RNGs, and for that you have to set them from R (or use
Rcpp::Function()).  Also see the docs on the R Math Library (Section 6.16 of
Writing R Extensions) which gives you special functions and RNGs and even
set_seed() but that is for use _apart from R_ which is NOT what we do here.

Thx, I will look at RcppZiggurrat package, and also try other C++ functions.
-----------------------------------------------------------------
-----------------

Hi Youssef,

Rcpp has sugar for the R random number generators. This is nice if you want
to let your users set the random seed in R code outside of the package for
reproducibility: http://gallery.rcpp.org/articles/random-number-generation/

Alternatively, if you enable C++11, there are some random number generators
in std: https://www.guyrutenberg.com/2014/05/03/c-mt19937-example/

Best,
--
Hao Ye

Thx, in my case, i have it initialize the number generator, so i will try
check the proposed link.
-----------------------------------------------------------------
-----------------


Best regards,
Youssef



2018-02-07 16:44 GMT+01:00 Hao Ye <hye at ucsd.edu>:

> Hi Youssef,
>
> Rcpp has sugar for the R random number generators. This is nice if you
> want to let your users set the random seed in R code outside of the package
> for reproducibility: http://gallery.rcpp.org/articles/
> random-number-generation/
>
> Alternatively, if you enable C++11, there are some random number
> generators in std: https://www.guyrutenberg.com/2014/05/03/c-mt19937-
> example/
>
> Best,
> --
> Hao Ye
>
> On Wed, Feb 7, 2018 at 10:20 AM, Hmamouche Youssef <
> hmamoucheyussef at gmail.com> wrote:
>
>> Hi,
>>
>> I get a note from R CMD check for an Rcpp package:
>>
>>   Found ‘_srand’, possibly from ‘srand’
>>
>>  Found no calls to: ‘R_registerRoutines’, ‘R_useDynamicSymbols’
>>
>>
>> Is there some an  equivalent function for srand from Rcpp, which i can
>> use it inside c++ code?
>>
>> Best regards,
>> Youssef
>>
>> _______________________________________________
>> 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
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20180207/0f457ed0/attachment-0001.html>


More information about the Rcpp-devel mailing list