31

I'd like to set seeds in R only locally (inside functions), but it seems that R sets seeds not only locally, but also globally. Here's a simple example of what I'm trying (not) to do.

myfunction <- function () {
  set.seed(2)
}

# now, whenever I run the two commands below I'll get the same answer
myfunction()
runif(1)

So, my questions are: why does R set the seed globally and not only inside my function? And how I can make R to set the seed only inside my function?

Manoel Galdino
  • 2,376
  • 6
  • 27
  • 40

2 Answers2

42

Something like this does it for me:

myfunction <- function () {
  old <- .Random.seed
  set.seed(2)
  res <- runif(1)
  .Random.seed <<- old
  res
}

Or perhaps more elegantly:

myfunction <- function () {
  old <- .Random.seed
  on.exit( { .Random.seed <<- old } )
  set.seed(2)
  runif(1)
}

For example:

> myfunction()
[1] 0.1848823
> runif(1)
[1] 0.3472722
> myfunction()
[1] 0.1848823
> runif(1)
[1] 0.4887732
Romain Francois
  • 17,432
  • 3
  • 51
  • 77
  • Thank you! I'll try your answer. Any idea why R has this unexpected (at least to me) side effect? – Manoel Galdino Jan 14 '13 at 18:30
  • 1
    +1 Beat me to it. @ManoelGaldino As to "why?" I don't think this is unexpected at all. Implementing a new, separate random generator for every function environment would be terribly complicated, I would think. And possibly carry some performance overhead. – joran Jan 14 '13 at 18:32
  • 4
    I do think someone with greater knowledge than I should comment about how this issue affect parallel processes. – IRTFM Jan 14 '13 at 18:56
  • 11
    There's a small problem with this answer. Object `.Random.seed` may not exist if the seed has not been set or `runif()` (or other functions accessing the random number generator) has not been called in the current R session. Thus before storing `.Random.seed` one needs to check its existence with `exists()`, and maybe invoke runif() if it doesn't. – Theodore Lytras Jan 14 '13 at 19:00
  • 3
    @TheodoreLytras In an ideal world, `set.seed` would return the old value of the seed. – hadley Jan 14 '13 at 20:46
  • 2
    That's an excellent answer. One question, won't **`<<--`** always push to parent frame? Shouldn't we use `assign` with `envir = globalenv` to ensure that `old` is pushed back to the global environment? – Konrad Oct 08 '18 at 07:28
  • How about `on.exit(set.seed(NULL))`? Then you don't need to store the old value? – Dan Lewer Jun 26 '19 at 11:29
0

Using @Romain Francois's answer, generalize as function:

withRandom <- function(expr, seed = 1) {
    old <- .Random.seed
    on.exit({.Random.seed <<- old})
    set.seed(seed)
    expr
}

Usage:

runif(2)
withRandom(seed = 2, {
    runif(1)
    runif(1)
})
runif(2)
withRandom(seed = 2, runif(2))
runif(2)

output:

> runif(2)
[1] 0.5776099 0.6309793
> withRandom(seed = 2, {
+     runif(1)
+     runif(1)
+ })
[1] 0.702374
> runif(2)
[1] 0.5120159 0.5050239
> withRandom(seed = 2, runif(2))
[1] 0.1848823 0.7023740
> runif(2)
[1] 0.5340354 0.5572494

Gwang-Jin Kim
  • 9,303
  • 17
  • 30