Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
302 views
in Technique[技术] by (71.8m points)

rcpp - When does using RNGScope make a difference?

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, because I've only ever seen it described as "ensures RNG state gets set/reset".

Then, I tested a bit, but I can't seem to come up with an example where doing this makes any difference. I used an example from here. My tests were:

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
NumericVector noscope() {
  Rcpp::Function rt("rt");
   return rt(5, 3);
}

// [[Rcpp::export]]
NumericVector withscope() {
   RNGScope scope;
   Rcpp::Function rt("rt");
   return rt(5, 3);
}

and then

set.seed(45)
noscope() # [1]  0.6438 -0.6082 -1.9710 -0.1402 -0.6482

set.seed(45)
withscope() # [1]  0.6438 -0.6082 -1.9710 -0.1402 -0.6482

set.seed(45)
rt(5, 3) # [1]  0.6438 -0.6082 -1.9710 -0.1402 -0.6482

So, my question is twofold. First, when does RNGScope make a difference, and what exactly does it do different from not using it? Second, does anyone have a code example which shows different results with and without it?

If RNGScope was deprecated in a newer release, then I'm sorry for asking.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 <Rcpp.h>

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...