Sample a random integer in Rcpp

只愿长相守 提交于 2019-12-09 16:35:58

问题


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.


回答1:


  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.



来源:https://stackoverflow.com/questions/14853334/sample-a-random-integer-in-rcpp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!