[英]Generating sample of integers in Rcpp
我想創建一個5個整數的隨機向量,范圍例如:1:10。 我只能使用基本的Rcpp。 (沒有C庫)
目前我有:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector test(){
NumericVector z(5);
for (int i=0; i<5 ++i)
z[i] = R::runif(1,10);
return z;
}
/***R
test()
*/
但:
它不是整數
它不是唯一的。
這可以用std::random_shuffle
簡潔地完成:
#include <Rcpp.h>
// [[Rcpp::export]]
Rcpp::IntegerVector sample_int() {
Rcpp::IntegerVector pool = Rcpp::seq(1, 10);
std::random_shuffle(pool.begin(), pool.end());
return pool[Rcpp::Range(0, 4)];
}
樣本輸出:
sample_int()
# [1] 9 2 5 1 7
sample_int()
# [1] 1 10 5 3 8
sample_int()
# [1] 5 9 3 2 8
而對於記錄,你的代碼並沒有返回整數,因為
::runif
返回double
值; 和 NumericVector
而不是IntegerVector
雖然在處理小范圍(如示例中使用的范圍(1,...,10)時無關緊要,但這種方法效率不高(特別是當采樣的元素數量遠遠小於繪圖池時) ,因為std::random_shuffle
了整個范圍。 有了幾個輔助函數,我們可以做得更好(假設std::rand
為了你的目的“足夠”隨機):
#include <Rcpp.h>
// C++ 98
template <typename Iter, typename T>
inline void iota(Iter first, Iter last, T value) {
while (first != last) {
*first++ = value++;
}
}
template <typename T>
inline T pop_random(std::vector<T>& v) {
typename std::vector<T>::size_type pos = std::rand() % v.size();
T res = v[pos];
std::swap(v[pos], v.back());
v.pop_back();
return res;
}
// [[Rcpp::export]]
Rcpp::IntegerVector sample_int2(int n, int min, int max) {
Rcpp::IntegerVector res(n);
std::vector<int> pool(max + 1 - min);
iota(pool.begin(), pool.end(), min);
for (R_xlen_t i = 0; i < n; i++) {
res[i] = pop_random(pool);
}
return res;
}
並概括原始解決方案以進行比較:
// [[Rcpp::export]]
Rcpp::IntegerVector sample_int(int n, int min, int max) {
Rcpp::IntegerVector pool = Rcpp::seq(min, max);
std::random_shuffle(pool.begin(), pool.end());
return pool[Rcpp::Range(0, n - 1)];
}
microbenchmark::microbenchmark(
"sample_int" = sample_int(100, 1, 1e6),
"sample_int2" = sample_int2(100, 1, 1e6),
times = 300L
)
# Unit: milliseconds
# expr min lq mean median uq max neval
# sample_int 20.639801 22.417594 23.603727 22.922765 23.735258 35.531140 300
# sample_int2 1.504872 1.689987 1.789866 1.755937 1.830249 2.863399 300
microbenchmark::microbenchmark(
"sample_int" = sample_int(1e5, 1, 1e6),
"sample_int2" = sample_int2(1e5, 1, 1e6),
times = 300L
)
# Unit: milliseconds
# expr min lq mean median uq max neval
# sample_int 21.08035 22.384714 23.295403 22.811011 23.282353 34.068462 300
# sample_int2 3.37047 3.761608 3.992875 3.945773 4.086605 9.134516 300
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.