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

G. Jay Kerns gkerns at ysu.edu
Fri May 15 19:08:06 CEST 2009

```>
> 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.)
>
> set.seed(-2^31+100000)
> system.time(whatseed(runif(1)))
>   user  system elapsed
>   1.53    0.00    1.53
>
> 2^32*(1.53/100000)/3600
>    => 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
> preserved.
>
> 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.
>
>             -s
>

Thanks very much to Warren and Stavros for their additional insight.
Putting all of this together, I think I am now ready to formulate my
question intelligently:

Using Sweave, I want to distribute randomly generated problems AND
answers to both teacher AND student.

More precisely, I want to distribute:
1) the .Rnw file
2) the .RData file saved near the end of the Sweave process.

I want it to be *easy* for the Instructor to change my seed and
generate new problems.

I want it to be *difficult* for students to figure out the seed and
automatically generate solutions on their own.

Of course, "difficult" is a relative term, since what is "difficult"
for them may well be "easy" for me, and what is difficult for me will
be trivial to cryptographers and some people on this list.  The
audience would be, say, upper division undergraduate students at a
public university.

What is clear so far: a brute force search of set.seed() is really
pretty easy and fast... even for students at this level.

However, relating to Duncan's second remark:  what if the Instructor
inserted an *unknown* very large number of calls to the RNG near the
beginning of the .Rnw (but after the set.seed)...  and did not
distribute this information to the students...  that would make it
much harder, yes?

Any ideas that are even better than this?

Conceivably, some of my students will be searching these archives in
the future;  please feel free to respond off-list if appropriate.

Jay

--

***************************************************
G. Jay Kerns, Ph.D.
Associate Professor
Department of Mathematics & Statistics
Youngstown State University
Youngstown, OH 44555-0002 USA
Office: 1035 Cushwa Hall
Phone: (330) 941-3310 Office (voice mail)
-3302 Department
-3170 FAX
E-mail: gkerns at ysu.edu
http://www.cc.ysu.edu/~gjkerns/

```