Difference between R::runif() and Rcpp::runif()

I'm learning to use Rcpp in R. Would you please explain me the difference between R::runif() and Rcpp::runif().

I mean 3 questions:

  1. Do these 2 functions produce the same stream of random numbers given that we set the same seed before running each of them ?

  2. Which function is preferable when using Rcpp ? I mean, it seems to me that the 2 functions produce the same thing, but Rcpp::runif() will run more fastly.

  3. How to call Rcpp::runif() in a .R file ? Is it true that the Rcpp::runif() can be called only from a .cpp file and not in R? (I mean, it seems to me that the function Rcpp::runif() is of extensively used to write other C++ functions, then I will import that function by sourcecpp() to use in R)

Thank you very much for your help!

Upvotes: 1

Views: 622

Answers (1)

Dirk is no longer here
Dirk is no longer here

Reputation: 368261

I suspect this question is a duplicate so I may close this but here goes:

  1. Yes they do. The whole point of the RNG interfaces is guaranteeing just that

  2. Entirely up to you. Sometimes you want to wrap or use a C API example and you have R::runif() for that. Sometimes you want efficient vector operations for which you have Rcpp::runif().

  3. You write a C++ function accessing the C++ API. Note that not all those functions will be faster than calling what R offers when what R offers is already vectorised. Your wrapping of Rcpp::runif() will not be much different in performance from calling stats::runif(). You use the C++ accessor in C++ code writing something you cannot easily get from R.

Edit: This Rcpp Gallery post has some background and examples.

Upvotes: 2

Related Questions