Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sample a random integer in Rcpp

Tags:

r

rcpp

This might be a very easy question, but I can't really find it. How can I sample a random integer between 1 and N in Rcpp? I know I can sample a uniform number and round it, or include the sample function to use in the C++ code, but both don't seem like the correct way of doing this.

like image 670
Sacha Epskamp Avatar asked Feb 13 '13 12:02

Sacha Epskamp


1 Answers

  1. AFIACR we do not have a "decent" (ie vectorised) sample() function in Rcpp-land, but Christian Gunning is working on one based on Armadillo via RcppArmadillo.

  2. You can of course sample from U(0,1), scale to N and to the rest. We have R::runif() for the scalar case and runif() in sugar which is vectorised.

Edit: As of 2017, both RcppArmadillo and Rcpp now have a sample() function.

like image 165
Dirk Eddelbuettel Avatar answered Oct 22 '22 09:10

Dirk Eddelbuettel