11

My R script calls sub-functions which contains set.seed(). What is the scope of the set.seed()? Will it also affect to main program that calls it?

More specificly

# main program
callsubfun()

... some statement ...

sample.int(100,20)



# sub function
callsubfun <- function(x,y,...){
   set.seed(100)
   ... do the work ...
   return(something)
}
1
  • 1
    I'm pretty sure that set.seed() is 'global'. It has the potential for ruining your pseudo-randomness.
    – IRTFM
    Commented Aug 26, 2014 at 18:56

4 Answers 4

13

set.seed is indeed global. But note this from the example in ?set.seed:

## If there is no seed, a "random" new one is created:
rm(.Random.seed); runif(1); .Random.seed[1:6]

This means you can call rm(.Random.seed, envir=.GlobalEnv) either at the end of your function or after you call the function to decouple the rest of the program from the call to set.seed in the function.

To see this in action, run the following code in two different R sessions. The outputs should be the same in both sessions. Then re-run the code again in two new R sessions with the rm line uncommented. You'll see the output in the two new sessions now differ, indicating that the call to set.seed in the function hasn't transferred the reproducibility to the main program.

subfun <- function() {
    set.seed(100)
    rnorm(1)
    #rm(.Random.seed, envir=.GlobalEnv)
}

subfun()
#[1] -0.5022

rnorm(1)
# [1] 0.1315
3

Here's why you should NOT do that:

> set.seed(100)
> rnorm(1)
[1] -0.5021924
> rnorm(1)
[1] 0.1315312

> rand <- function() set.seed(100)
> rand()

> rnorm(1)
[1] -0.5021924   # Ouch!
2
  • So you mean that the set.seed() function is suggested to be used only at the very top level and just once for the whole scripts. Is that correct?
    – lolibility
    Commented Aug 26, 2014 at 19:18
  • 4
    Yes, indeed. That is what your insightful question and that experiment above would imply. In fact, I'm going to suggest to R-Core that a warning saying so be added to the help page for set.seed. I think this behavior is implied by the description on that page but I think the implications should be made more explicit.
    – IRTFM
    Commented Aug 26, 2014 at 19:20
-1

set.seed function gives the specific random state for all code after itself. However, the code before it will not be affected. Here is a minimal example.

rnorm(1)
#> [1] -0.1020965
set.seed(123)
rnorm(1)
#> [1] -0.5604756
rnorm(1)
#> [1] -0.2301775
rnorm(1)
#> [1] 1.558708

Created on 2018-10-20 by the reprex package (v0.2.0).

rnorm(1)
#> [1] 0.4665633
set.seed(123)
rnorm(1)
#> [1] -0.5604756
rnorm(1)
#> [1] -0.2301775
rnorm(1)
#> [1] 1.558708

Created on 2018-10-20 by the reprex package (v0.2.0).

  1. You will find all code next to set.seed are fixed at a given random state.
  2. But the codes before set.seed are different between the two trials, which support my opnion.
-2

BondedDust's answer is ok, but not alway is Ouch!

You can use set.seed() in order to reproduce your algorithm. i.e. anyone can reproduce your results.

I recommend you use set.seed if you wanted to share your code. For example if you wanted some help in stackoverflow, we could reproduce exactly your code.

set.seed(123)
rand<-rnorm(10)
plot(density(rand))

If you use another seed, you get other results

set.seed(234)
rand<-rnorm(10)
plot(density(rand))

Both are correct but it could be easier to help you if the seed was known. By the way, just use set.seed once in your rutine because you could generate dependence in your random numbers. We desire independent random numbers in simulation.

1
  • 2
    This is does not answer the question. The question is whether using set.seed inside a function will affect an entire script (globally). Commented Aug 26, 2014 at 19:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.