Skip to content

Commit 3a0a11d

Browse files
committed
introduce an equivalent function to achieve set.seed(NULL) in R 2.15.x
1 parent 7eb8ddf commit 3a0a11d

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

R/globals.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
# R's lazy-loading package scheme causes the private seed to be cached in the
66
# package itself, making our PRNG completely deterministic. This line resets
77
# the private seed during load.
8-
withPrivateSeed(set.seed(NULL))
8+
withPrivateSeed(reinitializeSeed())
99
}

R/utils.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@ withPrivateSeed <- function(expr) {
8484
)
8585
}
8686

87+
# a homemade version of set.seed(NULL) for backward compatibility with R 2.15.x
88+
reinitializeSeed <- if (getRversion() >= '3.0.0') {
89+
function() set.seed(NULL)
90+
} else function() {
91+
if (exists('.Random.seed', globalenv()))
92+
rm(list = '.Random.seed', pos = globalenv())
93+
runif(1) # generate any random numbers so R can reinitialize the seed
94+
}
95+
8796
# Version of runif that runs with private seed
8897
p_runif <- function(...) {
8998
withPrivateSeed(runif(...))

0 commit comments

Comments
 (0)