Rcpp Gallery explains how to shuffle a vector using Rcpp.
However, it uses std::random_shuffle()
, which is no longer supported in C++17. I naively replaced std::random_shuffle()
with std::shuffle()
, but it did not work with C++17.
instantiation of function template specialization 'std::shuffle<std::__wrap_iter<int *>, int (&)(int)>' requested here
I suppose it's because std::random_shuffle()
takes RandomFunc
but std::shuffle()
in C++17 takes URBG
(reference).
How can we make this code work with C++17? (How can we define UniformRandomBitGenerator
using the R randomization feature?)
inline int randWrapper(const int n) { return floor(unif_rand()*n); }
// [[Rcpp::export]]
Rcpp::NumericVector randomShuffle(Rcpp::NumericVector a) {
// clone a into b to leave a alone
Rcpp::NumericVector b = Rcpp::clone(a);
std::random_shuffle(b.begin(), b.end(), randWrapper);
return b;
}
My code is more complex than this, but it uses the same trick. It works fine if I link Rcpp with C++11 but not with C++17.
I would like to get the same results from C++17 as well (i.e., I would like to use an R randomizer and make sure the results won't change by changing it from C++11 to C++17).
Now CRAN does not accept C++11 so I need to fix it.