简体   繁体   English

在Rcpp中采样随机整数

[英]Sample a random integer in 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? 如何在Rcpp中采样1到N之间的随机整数? 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. 我知道我可以对一个统一的数字进行采样并对其进行舍入,或者包含要在C ++代码中使用的sample函数,但两者似乎都不是正确的方法。

  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. AFIACR我们在Rcpp-land中没有“体面”(即矢量化) sample()函数,但Christian Gunning正在通过RcppArmadillo开发一个基于Armadillo的模型。

  2. You can of course sample from U(0,1), scale to N and to the rest. 您当然可以从U(0,1)中抽样,缩放到N,然后到其余部分。 We have R::runif() for the scalar case and runif() in sugar which is vectorised. 我们在标量情况下使用R::runif() ,在矢量化中使用糖中的runif()

Edit: As of 2017, both RcppArmadillo and Rcpp now have a sample() function. 编辑:截至2017年,RcppArmadillo和Rcpp现在都有sample()函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM