5

I have used the following as an example

x <- 1:12
sample(x)

Whenever I restart RStudio or Rgui and run that code, I always get the same result:

 [1]  7  3 10  8  2  9 11  4 12  5  1  6

Is there a way to change this annoying behavior?

> sessionInfo()
R version 3.1.0 (2014-04-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=Greek_Greece.1253  LC_CTYPE=Greek_Greece.1253   
[3] LC_MONETARY=Greek_Greece.1253 LC_NUMERIC=C                 
[5] LC_TIME=Greek_Greece.1253    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base 
gd047
  • 29,749
  • 18
  • 107
  • 146
  • 1
    Do you have other things loaded automatically at startup? – A5C1D2H2I1M1N2O1R2T1 Jul 12 '14 at 08:42
  • hm... it's not Rstudio. RGui does the same. I deleted everything I had in Rprofile, but still I get the same result. – gd047 Jul 12 '14 at 08:48
  • Do you have a `.Random.seed` in your workspace that is being restored when you start R? `ls(all=TRUE)` will show stuff beginning with a dot, which are normally hidden. – Spacedman Jul 12 '14 at 10:42
  • I do, indeed. > ls(all=TRUE) [1] ".Random.seed" – gd047 Jul 12 '14 at 10:53
  • How can i get rid of it? `rm(list = ls(all=TRUE))` doesn't seem to help. – gd047 Jul 12 '14 at 11:08
  • Have you tried starting R in `vanilla` mode? – Roland Jul 12 '14 at 13:00
  • 1
    The local `.Rprofile` file is not the only file potentially sourced by R at startup. All locations are documented in `?Sartup`. This http://stackoverflow.com/questions/13735745/locate-the-rprofile-file-generating-default-options/13736073#13736073 might help you list such files. Search them for a call to `set.seed`. – flodel Jul 12 '14 at 15:05
  • @flodel That code listed only my .Rprofile file which does contain a set.seed command. – gd047 Jul 12 '14 at 16:11
  • 6
    `rm(.Random.seed)` and save workspace (`save.image()`). Note this behaviour is by design. If you restore a workspace, you restore the RNG seed, restoring the state of things. – Spacedman Jul 12 '14 at 22:46
  • @Spacedman Post that as an answer. – Thomas Aug 05 '14 at 11:55

1 Answers1

1

Explicitly include a line: set.seed(NULL)

ddunn801
  • 1,900
  • 1
  • 15
  • 20