diff --git a/R/seed.R b/R/seed.R index 2191241c..28f4997e 100644 --- a/R/seed.R +++ b/R/seed.R @@ -44,9 +44,9 @@ local_seed <- function(seed, defer(envir = .local_envir, { if (is.null(old_seed)) { - on.exit(rm_seed(), add = TRUE) + rm_seed() } else { - on.exit(set_seed(old_seed), add = TRUE) + set_seed(old_seed) } }) diff --git a/tests/testthat/test-seed.R b/tests/testthat/test-seed.R index b4da2846..172f5768 100644 --- a/tests/testthat/test-seed.R +++ b/tests/testthat/test-seed.R @@ -69,6 +69,17 @@ test_that("local_seed works as expected", { expect_false(x == y) }) +test_that("local_seed doesn't modify `.Random.seed` (#286)", { + f1 <- function(n, seed) { + local_seed(seed) + rnorm(n) + } + set.seed(42) + seed3 <- .Random.seed + rslt3 <- f1(5, 41) + expect_identical(seed3, .Random.seed) +}) + test_that("with_preserve_seed preserves empty seed", { rm_seed() with_preserve_seed(runif(1))