首页 > 解决方案 > 如何使用 future.callr 抑制随机数生成警告?

问题描述

future.callr每次请求未来时,我都会使用它创建一个新线程(?),因此它是单独计算的,主 R 脚本可以继续前进。

当我的期货回来时,我收到以下警告:

    Warning message:
UNRELIABLE VALUE: Future (‘<none>’) unexpectedly generated random numbers without specifying argument 'seed'. There is a risk that those random numbers are not statistically sound and the overall results might be invalid. To fix this, specify 'seed=TRUE'. This ensures that proper, parallel-safe random numbers are produced via the L'Ecuyer-CMRG method. To disable this check, use 'seed=NULL', or set option 'future.rng.onMisuse' to "ignore".

在我正在运行的实际代码中,它只是加载一些数据,我不知道为什么(或关心编辑:我确实关心,请参阅下面的评论)它正在生成随机数。如何阻止显示该警告(修复 rng 生成或忽略它)?

我有很多关于期货的行,所以我希望能够以某种方式在开头设置选项,而不必将其添加到每一行。

这是一个示例,我试图忽略该警告。

library(future.callr)

set.seed(1234567)
future.seed = TRUE

#normal random number - no problem
a<-runif(1)
print(a)

#random number in future, using callr plan
plan(callr, future.rng.onMisuse = 'ignore')

b %<-% runif(1)
print(b)

标签: rfuturecallrfuture.callr

解决方案


help("%seed%", package = "future")

您可以%seed%像下面这样使用:

b %<-% runif(1) %seed% TRUE # seed is set by future pkg
print(b)

# or
b %<-% runif(1) %seed% 1234567 # your seed
print(b)

或禁用检查

options(future.rng.onMisuse = "ignore")
b %<-% runif(1)
print(b)

推荐阅读