[Rcpp-devel] Setting seed using Rcpp

Dirk Eddelbuettel edd at debian.org
Wed Feb 7 16:36:28 CET 2018

On 7 February 2018 at 16:20, Hmamouche Youssef 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?

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.


http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org

More information about the Rcpp-devel mailing list