Can I get.seed() somehow?

If you didn't keep the seed, there's no general way to "roll back" the random number generator to a previous state after you've observed a random draw. Going forward, what you may want to do is save the value of .Random.seed along with the results of your computations. Something like this.

x <- .Random.seed
result <- <your code goes here>
attr(result, "seed") <- x

Then you can reset the PRNG as follows; result2 should be the same as result.

.Random.seed <- attr(result, "seed")
result2 <- <your code goes here>

Hong's answer above is robust. For quick and dirty solutions, where I just re-execute a whole script until I get interesting behavior, I randomly pick an integer, print it out, then use that as a seed. If my particular run has interesting behavior, I note that seed:

eff_seed <- sample(1:2^15, 1)
print(sprintf("Seed for session: %s", eff_seed))
set.seed(eff_seed)

Tags:

R