`sample()` gives different values with same `set.seed()`
Asked Answered
N

1

15

I was creating some random samples and plotting them and noticed a strange behavior. Sampled values were different after loading ggplot2:

set.seed(111)
library(ggplot2)
sample(1:10, 10)
# [1]  8  4  5  3  7  1  6  2 10  9

set.seed(111)
sample(1:10, 10)
#  [1]  6  7  3  4  8 10  1  2  9  5

I can avoid this behavior easily enough, but is there any reason for ggplot2 to change the seed value?

Norbert answered 7/3, 2013 at 1:47 Comment(2)
It isn't about changing the seed value - it's about getting a random number (before you).Hiller
Note that if you'd tested the repeatability of both results, it would have been a strong hint that ggplot2 was doing something (repeatable) with the random seed. Experiments are always good :-)Subtitle
C
16

I think I saw some discussion of this in one of the R chat rooms: ggplot2 calls the random number generator in order to decide whether/which tip it wants to offer.

In particular, this is ggplot2:::.onAttach:

function (...) 
{
    if (!interactive() || stats::runif(1) > 0.1) 
        return()
    tips <- c("Need help? Try the ggplot2 mailing list: http://groups.google.com/group/ggplot2.", 
        paste("Find out what's changed in ggplot2 with\n", "news(Version == \"", 
            utils::packageVersion("ggplot2"), "\", package = \"ggplot2\")", 
            sep = ""), "Use suppressPackageStartupMessages to eliminate package startup messages.")
    tip <- sample(tips, 1)
    packageStartupMessage(tip)
}

It's sort of amusing that one of the randomly generated tips tells you how to turn off the tips ...

Cochineal answered 7/3, 2013 at 1:49 Comment(3)
I'v never experienced ggplot2 giving any tips at startup or on attach, but that makes sense if it has that option. Thanks for the clarification.Norbert
Well, you only get a tip 10% of the time ... and only in recent versions, I think.Cochineal
Shouldn't it tip at least 15%?Sirajuddaula

© 2022 - 2024 — McMap. All rights reserved.