When does using RNGScope make a difference?

后端 未结 2 1849
予麋鹿
予麋鹿 2020-12-16 23:28

In Rcpp documentation, I often find the recommendation to place Rcpp::RNGScope scope; before using random draws within Rcpp. I wondered what exactly this does,

2条回答
  •  -上瘾入骨i
    2020-12-17 00:04

    When using Rcpp attributes, the automagically generated interface to your code will automatically insert the appropriate construction of the RNGScope object -- so it's already being done for you behind the scenes in this case. For example, if you write sourceCpp(..., verbose = TRUE), you'll see output like this:

    Generated extern "C" functions 
    --------------------------------------------------------
    
    
    #include 
    
    RcppExport SEXP sourceCpp_38808_timesTwo(SEXP xSEXP) {
    BEGIN_RCPP
        Rcpp::RObject __result;
        Rcpp::RNGScope __rngScope;
        Rcpp::traits::input_parameter< NumericVector >::type x(xSEXP);
        __result = Rcpp::wrap(timesTwo(x));
        return __result;
    END_RCPP
    }
    

    Note the automatic construction of the RNGScope object.

    You only need to construct that object manually if you are operating outside of the realm of Rcpp attributes.

提交回复
热议问题