on-internet
← Community

Strange behaviour of Rcpp when generating random numbers

Stack Overflow ·

I have a routine written in Rcpp that is supposed to generate random numbers. Strangely enough, if I also call a completely unrelated routine, it always returns the same number. Here is a toy example: #include <Rcpp.h> using namespace Rcpp; // [[Rcpp::export]] void f() {} // [[Rcpp::export]] void test(Function g) { for(int j=0;j<5;++j) { Rcout<<R::runif(0,1)<<" "; } Rcout<<"\n"; for(int j=0;j<5;++j) { g(); Rcout<<R::runif(0,1)<<" "; } } test(f) When running test the first loop generates different random numbers but the second always generates the same. Both loops start with the same number. So

Comments

Sign in or sign up to comment and upvote.

No comments yet.