[R] can you tell what .Random.seed *was*?

Stavros Macrakis macrakis at alum.mit.edu
Fri May 15 18:07:12 CEST 2009

On Thu, May 14, 2009 at 3:36 PM, G. Jay Kerns <gkerns at ysu.edu> wrote:
> set.seed(something)
> x <- rnorm(100)
> y <- runif(500)
> # bunch of other stuff
> Now, I give you a copy of my script.R (with the set.seed statement
> removed, of course) together with the .RData file that was generated
> by the save.image() command.
> 1) can you tell me what my original set.seed() value was?...
> 2) is it possible *in principle* to figure out what set.seed was,
> given the above?

Set.seed takes an integer argument, that is, 2^32-1 distinct values
(cf NA_integer_), so the very simplest approach, brute-force search,
has a hope of working:

whatseed <- function (v)  {
   i <- as.integer(-2^31+1); max <- as.integer(2^31-1)
   while (i<max) { set.seed(i); if (runif(1)==v) return(i); i<-i+1 }

> (OK, being able to figure it out in 2*10^68 years
> doesn't count, but within a couple months is acceptable.)

   user  system elapsed
   1.53    0.00    1.53

    => 18.25
18 hours

> 3) does the answer change if there is a
> remove(.Random.seed)
> command right before the save.image() command?

Depending on which RNG algorithm (RNGkind) you use, there may be
cryptographic techniques that are more efficient than brute-force
search, especially if the full internal state (.Random.seed) is

This all assumes that the seed is set *only* with set.seed.  If
.Random.seed is modified directly, there are many more possibilities
for most of the RNGs.


More information about the R-help mailing list