Tim
Tim

Reputation: 7474

Is (and when) RNGScope scope needed in Rcpp code?

When RNGScope scope is needed when using Rcpp? In general, you need it when using C RNG functions. In some places you can read that it is not needed when using Rcpp, examples in Rcpp documentation use it, some examples by Dirk Eddelbuettel use it, while others do not like this one or this do not. So in the end I'm confused...

When exactly is it needed and when not? Does it make a difference if I use Rcpp::runif(), R::runif(), or R::unif_rand()? I'm interested mostly in using Rcpp within R package rather then calling standalone code.

Upvotes: 1

Views: 303

Answers (1)

Dirk is no longer here
Dirk is no longer here

Reputation: 368509

In short:

  • whenever you call the RNGs of the C API of R, you need to save and later re-set state
  • RNGScope automates this for you as it is so darn useful (and generally inexpensive)
  • whenever you use Rcpp Attributes, it inserts RNGScope (as you can see when you turn on verbose=TRUE

So in general you don't need to anything manual -- unless you go really old school and write all code directly, foregoing the glue provided by Rcpp.

And if you use Rcpp, it doesn't matter whether you use scalar interfaces from the R:: namespace or the vectorized Rcpp Sugar onces via Rcpp::. RNGScope will be there for you.

Upvotes: 4

Related Questions