当使用R的 runif 并通过set.seed选项设置kind = NULL的种子时,我们面临着一种奇怪的情况(除非我没有记错,否则它将解析为kind = "default";默认为"Mersenne-Twister")。

在调用runif之前,我们使用上游系统生成的(8位)唯一ID设置种子:

seeds = c(
  "86548915", "86551615", "86566163", "86577411", "86584144",
  "86584272", "86620568", "86724613", "86756002", "86768593", "86772411",
  "86781516", "86794389", "86805854", "86814600", "86835092", "86874179",
  "86876466", "86901193", "86987847", "86988080")

random_values = sapply(seeds, function(x) {
  set.seed(x)
  y = runif(1, 17, 26)
  return(y)
})

这样就可以将极其紧密地组合在一起的值。
> summary(random_values)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
  25.13   25.36   25.66   25.58   25.83   25.94

当我们使用runif时,kind = "Knuth-TAOCP-2002"的这种行为消失了,并且我们得到的值似乎更均匀地散布了。
random_values = sapply(seeds, function(x) {
  set.seed(x, kind = "Knuth-TAOCP-2002")
  y = runif(1, 17, 26)
  return(y)
})

输出省略。

这里最有趣的是,这不会在Windows上发生-仅在Ubuntu上(以下Ubuntu和Windows的sessionInfo输出)上发生。

Windows输出:
> seeds = c(
+   "86548915", "86551615", "86566163", "86577411", "86584144",
+   "86584272", "86620568", "86724613", "86756002", "86768593", "86772411",
+   "86781516", "86794389", "86805854", "86814600", "86835092", "86874179",
+   "86876466", "86901193", "86987847", "86988080")
>
> random_values = sapply(seeds, function(x) {
+   set.seed(x)
+   y = runif(1, 17, 26)
+   return(y)
+ })
>
> summary(random_values)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
  17.32   20.14   23.00   22.17   24.07   25.90

有人可以帮助您了解发生了什么吗?

的Ubuntu
R version 3.4.0 (2017-04-21)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.2 LTS

Matrix products: default
BLAS: /usr/lib/libblas/libblas.so.3.6.0
LAPACK: /usr/lib/lapack/liblapack.so.3.6.0

locale:
[1] LC_CTYPE=en_US.UTF-8          LC_NUMERIC=C
 [3] LC_TIME=en_US.UTF-8           LC_COLLATE=en_US.UTF-8
 [5] LC_MONETARY=en_US.UTF-8       LC_MESSAGES=en_US.UTF-8
 [7] LC_PAPER=en_US.UTF-8          LC_NAME=en_US.UTF-8
 [9] LC_ADDRESS=en_US.UTF-8        LC_TELEPHONE=en_US.UTF-8
[11] LC_MEASUREMENT=en_US.UTF-8    LC_IDENTIFICATION=en_US.UTF-8

attached base packages:
[1] parallel  stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] RMySQL_0.10.8               DBI_0.6-1
 [3] jsonlite_1.4                tidyjson_0.2.2
 [5] optiRum_0.37.3              lubridate_1.6.0
 [7] httr_1.2.1                  gdata_2.18.0
 [9] XLConnect_0.2-12            XLConnectJars_0.2-12
[11] data.table_1.10.4           stringr_1.2.0
[13] readxl_1.0.0                xlsx_0.5.7
[15] xlsxjars_0.6.1              rJava_0.9-8
[17] sqldf_0.4-10                RSQLite_1.1-2
[19] gsubfn_0.6-6                proto_1.0.0
[21] dplyr_0.5.0                 purrr_0.2.4
[23] readr_1.1.1                 tidyr_0.6.3
[25] tibble_1.3.0                tidyverse_1.1.1
[27] rBayesianOptimization_1.1.0 xgboost_0.6-4
[29] MLmetrics_1.1.1             caret_6.0-76
[31] ROCR_1.0-7                  gplots_3.0.1
[33] effects_3.1-2               pROC_1.10.0
[35] pscl_1.4.9                  lattice_0.20-35
[37] MASS_7.3-47                 ggplot2_2.2.1

loaded via a namespace (and not attached):
[1] splines_3.4.0      foreach_1.4.3      AUC_0.3.0          modelr_0.1.0
 [5] gtools_3.5.0       assertthat_0.2.0   stats4_3.4.0       cellranger_1.1.0
 [9] quantreg_5.33      chron_2.3-50       digest_0.6.10      rvest_0.3.2
[13] minqa_1.2.4        colorspace_1.3-2   Matrix_1.2-10      plyr_1.8.4
[17] psych_1.7.3.21     XML_3.98-1.7       broom_0.4.2        SparseM_1.77
[21] haven_1.0.0        scales_0.4.1       lme4_1.1-13        MatrixModels_0.4-1
[25] mgcv_1.8-17        car_2.1-5          nnet_7.3-12        lazyeval_0.2.0
[29] pbkrtest_0.4-7     mnormt_1.5-5       magrittr_1.5       memoise_1.0.0
[33] nlme_3.1-131       forcats_0.2.0      xml2_1.1.1         foreign_0.8-69
[37] tools_3.4.0        hms_0.3            munsell_0.4.3      compiler_3.4.0
[41] caTools_1.17.1     rlang_0.1.1        grid_3.4.0         nloptr_1.0.4
[45] iterators_1.0.8    bitops_1.0-6       tcltk_3.4.0        gtable_0.2.0
[49] ModelMetrics_1.1.0 codetools_0.2-15   reshape2_1.4.2     R6_2.2.0
[53] knitr_1.15.1       KernSmooth_2.23-15 stringi_1.1.5      Rcpp_0.12.11

视窗
> sessionInfo()
R version 3.3.2 (2016-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

locale:
[1] LC_COLLATE=English_India.1252  LC_CTYPE=English_India.1252    LC_MONETARY=English_India.1252
[4] LC_NUMERIC=C                   LC_TIME=English_India.1252

attached base packages:
[1] graphics  grDevices utils     datasets  grid      stats     methods   base

other attached packages:
 [1] bindrcpp_0.2         h2o_3.14.0.3         ggrepel_0.6.5        eulerr_1.1.0         VennDiagram_1.6.17
 [6] futile.logger_1.4.3  scales_0.4.1         FinCal_0.6.3         xml2_1.0.0           httr_1.3.0
[11] wesanderson_0.3.2    wordcloud_2.5        RColorBrewer_1.1-2   htmltools_0.3.6      urltools_1.6.0
[16] timevis_0.4          dtplyr_0.0.1         magrittr_1.5         shiny_1.0.5          RODBC_1.3-14
[21] zoo_1.8-0            sqldf_0.4-10         RSQLite_1.1-2        gsubfn_0.6-6         proto_1.0.0
[26] gdata_2.17.0         stringr_1.2.0        XLConnect_0.2-12     XLConnectJars_0.2-12 data.table_1.10.4
[31] xlsx_0.5.7           xlsxjars_0.6.1       rJava_0.9-8          readxl_0.1.1         googlesheets_0.2.1
[36] jsonlite_1.5         tidyjson_0.2.1       RMySQL_0.10.9        RPostgreSQL_0.4-1    DBI_0.5-1
[41] dplyr_0.7.2          purrr_0.2.3          readr_1.1.1          tidyr_0.7.0          tibble_1.3.3
[46] ggplot2_2.2.0        tidyverse_1.0.0      lubridate_1.6.0

loaded via a namespace (and not attached):
 [1] gtools_3.5.0         assertthat_0.2.0     triebeard_0.3.0      cellranger_1.1.0     yaml_2.1.14
 [6] slam_0.1-40          lattice_0.20-34      glue_1.1.1           chron_2.3-48         digest_0.6.12.1
[11] colorspace_1.3-1     httpuv_1.3.5         plyr_1.8.4           pkgconfig_2.0.1      xtable_1.8-2
[16] lazyeval_0.2.0       mime_0.5             memoise_1.0.0        tools_3.3.2          hms_0.3
[21] munsell_0.4.3        lambda.r_1.1.9       rlang_0.1.1          RCurl_1.95-4.8       labeling_0.3
[26] bitops_1.0-6         tcltk_3.3.2          gtable_0.2.0         reshape2_1.4.2       R6_2.2.0
[31] bindr_0.1            futile.options_1.0.0 stringi_1.1.2        Rcpp_0.12.12.1

最佳答案

注意:此答案总结了在R-devel邮件列表上进行的有关此问题的讨论的元素。我只是试图捕获和总结最初在此处表达的想法。

尽管您保证这些数字不是特殊构造的边缘情况,但它们的所有外观均是如此。这是原始序列以及检查产生的值分布的代码:

seeds = c(
    86548915, 86551615, 86566163, 86577411, 86584144, 86584272,
    86620568, 86724613, 86756002, 86768593, 86772411, 86781516,
    86794389, 86805854, 86814600, 86835092, 86874179, 86876466,
    86901193, 86987847, 86988080)
checkit <- function(seeds) {
    sapply(seeds, function(x) {
        set.seed(x)
        y = runif(1, 17, 26)
        return(y)
    })}

原始序列显示出令人惊讶的微小变化,如下所示:
  summary(checkit(seeds+0))
  ## Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
  ##25.13   25.36   25.66   25.58   25.83   25.94

原始序列确实存在一些特殊之处,因为对其进行的最小修改不会产生相同的令人惊讶的结果:
summary(checkit(seeds+1))
## Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
## 17.18   19.65   22.75   22.02   24.37   25.79

summary(checkit(seeds-1))
## Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
##17.15   18.44   19.92   20.77   22.97   25.95

在原始序列所涵盖范围内的所有种子中,预期数量产生的值在观察范围内:
possible.seeds <- min(seeds):max(seeds)

s25 <- Filter(function(s){
    set.seed(s)
    x <- runif(1,17,26)
    x > 25.12 & x < 25.95},
    possible.seeds)

length(s25)/length(possible.seeds)
##[1] 0.09175801

但是,原始序列中的所有值都在此子集中(当然我们已经知道了……)。
table(seeds %in% s25)

##TRUE
##  21

所有这些都指出了原始序列实际上是(也许是无意间)特殊构造的边缘盒的可能性。

10-04 11:19