Site icon R-bloggers

Guess the Random Seed

[This article was first published on Statistics, R, Graphics and Fun » R Language, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Stephanie asked in 511 today if we were able to get the random seed which was set by set.seed() but we were only given the random numbers (without knowing the seed). This kind of “hacker” questions sound interesting. One dirty solution should be the brute-force method, e.g:

# x: the random vector;
# FUN: the function that generates random numbers with the first argument
#      being the length of random numbers
# seed: candidate seeds to be tried one by one
# ...: other arguments to be passed to FUN
find.seed = function(x, FUN = rnorm, seed = 0:10000, ...) {
    res = NULL
    for (i in seed) {
        set.seed(i)
        rx = FUN(length(x), ...)
        # all() can be changed to all.equal() to obtain a rough solution
        #     allowing a little bit numeric errors
        if (all(x == rx)) {
            res = i
            break
        }
    }
    res
}

Of course it is not a good solution, although it might work:

> set.seed(1)
> test = rnorm(30)
> find.seed(test)
[1] 1
> set.seed(1234)
> test = rnorm(30)
> find.seed(test)
[1] 1234
> set.seed(987)
> test = runif(30, min = -1, max = 2)
> find.seed(test, runif, min = -1, max = 2)
[1] 987

Beside the probably very long computation time, the other disadvantage is we need to know the range of the seed. This could be obtained by some “prior” information. For instance, we may guess Dr Nettleton will not set a random seed greater than one million or less than 0.

Usually the random seed is obtained from a certain state of the computer such as the system time. I have an old example in this post: Random Number Generation on the Glasses (A Natural RNG).

Related Posts

To leave a comment for the author, please follow the link and comment on their blog: Statistics, R, Graphics and Fun » R Language.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.