In the Rcpp documentation, I often find a recommendation to host Rcpp::RNGScope scope; before using a random draw in rcpp. I wondered what exactly this is happening because I only ever saw it described as "ensuring that the RNG state gets set / reset".
Then I tested a bit, but it seems I can’t come up with an example where it matters. I used an example here . My tests:
#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 is it different from not using it? Secondly, does anyone have an example of code that shows different results with and without it?
If RNGScope is deprecated in a newer version, I wish I asked.
source share