Rcpp 代码中是否(以及何时)需要 RNGScope 范围?
Is (and when) RNGScope scope needed in Rcpp code?
使用Rcpp时什么时候需要RNGScope scope
?一般来说,你 need it when using C RNG functions. In some places you can read that it is when using Rcpp, examples in Rcpp documentation use it, some examples by Dirk Eddelbuettel use it, while others do not like this one or this 不会。所以最后我很困惑...
具体什么时候需要,什么时候不需要?如果我使用 Rcpp::runif()
、R::runif()
或 R::unif_rand()
会有什么不同吗?我最感兴趣的是在 R 包中使用 Rcpp 而不是调用独立代码。
简而言之:
- 每当调用R的C API的RNG时,需要保存并稍后重新设置状态
RNGScope
会为您自动执行此操作,因为它非常有用(而且通常很便宜)
- 每当您使用 Rcpp 属性时,它都会插入
RNGScope
(正如您在打开 verbose=TRUE
时看到的那样
所以一般来说你不需要任何手动操作——除非你真的老派直接编写所有代码,放弃 Rcpp 提供的胶水。
如果您使用 Rcpp,那么无论您是使用 R::
命名空间中的标量接口还是通过 Rcpp::
向量化的 Rcpp Sugar
一次都没有关系。 RNGScope
会等你的。
使用Rcpp时什么时候需要RNGScope scope
?一般来说,你 need it when using C RNG functions. In some places you can read that it is
具体什么时候需要,什么时候不需要?如果我使用 Rcpp::runif()
、R::runif()
或 R::unif_rand()
会有什么不同吗?我最感兴趣的是在 R 包中使用 Rcpp 而不是调用独立代码。
简而言之:
- 每当调用R的C API的RNG时,需要保存并稍后重新设置状态
RNGScope
会为您自动执行此操作,因为它非常有用(而且通常很便宜)- 每当您使用 Rcpp 属性时,它都会插入
RNGScope
(正如您在打开verbose=TRUE
时看到的那样
所以一般来说你不需要任何手动操作——除非你真的老派直接编写所有代码,放弃 Rcpp 提供的胶水。
如果您使用 Rcpp,那么无论您是使用 R::
命名空间中的标量接口还是通过 Rcpp::
向量化的 Rcpp Sugar
一次都没有关系。 RNGScope
会等你的。