set.seed() 函数与 sample() 函数结合使用是否始终提供相同的样本,而与使用的硬件无关?

Does the set.seed() function in combination with the sample() function provides always the same sample independent of the used hardware?

I 运行 sample() 命令与 set.seed() 命令相结合,始终获得相同的样本,并且一切正常。然而,在不同的笔记本电脑上应用相同的命令会得到不同的样本。有谁知道发生了什么事吗?

我也尝试了 set.seed()rnorm() 的组合,令人惊讶的是我在两台笔记本电脑上得到了完全相同的 运行dom 数字。

set.seed(123)
sample(LETTERS,6)

set.seed(123)
rnorm(6,1,1)

我预计在两台笔记本电脑上都会显示结果 "H" "T" "J" "U" "W" "A"。但是,一台笔记本电脑显示结果 "O" "S" "N" "C" "J" "R"。

set.seed(123)
rnorm(6,1,1)

已制作

0.4395244 0.7698225 2.5587083 1.0705084 1.1292877 2.7150650

在两台笔记本电脑上。

这不是由于 cross-hardware 重现性问题,而是 R 3.6.0 中引入的差异——您必须在 R 3.6.0 上有一台机器,在早期版本上有另一台机器。来自 help("set.seed")(来自 R 3.6.0):

Usage

...
set.seed(seed, kind = NULL, normal.kind = NULL, sample.kind = NULL)
...

Details

sample.kind can be "Rounding" or "Rejection", or partial matches to these. The former was the default in versions prior to 3.6.0: it made sample noticeably non-uniform on large populations, and should only be used for reproduction of old results. See PR#17494 for a discussion.

观察以下内容以了解机器中的区别(也就是说,这表明这不是cross-hardware问题,因为我只在一台机器上这样做):

set.seed(123, sample.kind = "Rejection") # Default in R 3.6.0
sample(LETTERS, 6)
# [1] "O" "S" "N" "C" "J" "R"

set.seed(123, sample.kind = "Rounding") # Default in R < 3.6.0
sample(LETTERS, 6)
# [Warning omitted]
# [1] "H" "T" "J" "U" "W" "A"