c++ - C++ 等价于 R 的 set.seed() 函数
问题描述
我想在set.seed()
C++ 函数中做相当于 R 的事情。我该怎么做呢?
请注意,我通过 Rcpp 包使这个 C++ 函数在 R 中可用。我想myfun(seed=42)
每次在 R 中调用它时都返回相同的结果。我不想在每次set.seed()
调用之前在 R 中设置 rng 种子。myfun
C++ 文件:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
double myfun(double seed) {
std::srand(seed); // <-- this doesn't do what i want
return R::rnorm(0,1);
}
/*** R
myfun(42)
myfun(42)
*/
这当前返回:
> myfun(42)
[1] 0.737397
> myfun(42)
[1] -0.02764103
我想要的是:
> myfun(42)
[1] 0.737397
> myfun(42)
[1] 0.737397
解决方案
推荐阅读
- xml - 使用 ansible 从列表中读取 XML 属性
- java - 如何在Java中将列表列表转换为二维数组
- elasticsearch - 如何正确删除 AWS ElasticSearch 索引以释放磁盘空间
- c++ - 我可以使用 declval 构造未使用的返回吗?
- c# - 在 C# 中为 Excel 生成名称
- json - RAD Studio XE5 JSON 支持
- javascript - 根据当前月份动态选择日期 - 选择列表
- sockets - 如何从 rp_remote_acquire 访问数据?
- java - Java 1.8.40 路径不与任何信任锚链接
- javascript - 编写将过滤输入的用户名的正则表达式