Skip to content

Commit e96193a

Browse files
committed
Do .Random.seed restoring correctly
1 parent 3ff9075 commit e96193a

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

R/random.R

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,17 @@
2222
#'
2323
#' @export
2424
repeatable <- function(rngfunc, seed = runif(1, 0, .Machine$integer.max)) {
25+
force(seed)
26+
2527
function(...) {
26-
currentSeed <- .Random.seed
27-
on.exit(.Random.seed <- currentSeed)
28+
# When we exit, restore the seed to its original state
29+
if (exists('.Random.seed', where=globalenv())) {
30+
currentSeed <- get('.Random.seed', pos=globalenv())
31+
on.exit(assign('.Random.seed', currentSeed, pos=globalenv()))
32+
}
33+
else {
34+
on.exit(rm('.Random.seed', pos=globalenv()))
35+
}
2836

2937
set.seed(seed)
3038

0 commit comments

Comments
 (0)