0

It seems that, the sample functions from RcppArmadillo and R return the same outputs when I set fixed the seed for each sample. However, I get different outcomes in my case. Here is my code

// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
#include <RcppArmadilloExtensions/sample.h>

using namespace Rcpp;
using namespace arma;
using namespace std;

// [[Rcpp::export]]
NumericVector f(int N, NumericVector x)
{return Rcpp::RcppArmadillo::sample(x, N, false);}


/*** R
set.seed(123)
f(5, 1:5)

[1] 2 4 5 3 1

set.seed(123)
sample(1:5, 5, FALSE)

[1] 3 2 5 4 1
*/

Does someone know why?

Ari.stat
  • 463
  • 4
  • 13
  • 1
    Possible duplicate of [sample function gives different result in console and in knitted document when seed is set](https://stackoverflow.com/questions/56268011/sample-function-gives-different-result-in-console-and-in-knitted-document-when-s). There are probably other/more appropriate duplicates ... – Ralf Stubner Oct 30 '19 at 17:08
  • 3
    Just try `set.seed(123, sample.kind = "Rounding"); sample(1:5, 5, FALSE)` in R. The `RcppArmadillo::sample` (and `Rcpp::sample`) functions still use the older "Rounding" algorithm. – Ralf Stubner Oct 30 '19 at 17:09
  • 1
    See also https://github.com/RcppCore/RcppArmadillo/issues/250 and https://github.com/RcppCore/Rcpp/issues/945. – Ralf Stubner Oct 30 '19 at 19:42

0 Answers0