# Set seed before each code block or once per project?

It is standard advice to set a random seed so that results can be reproduced. However, since the seed is advanced as pseudo-random numbers are drawn, the results could change if any piece of code draws an additional number.

At first glance, version control looks to be a solution to this, as it would at least allow you to go back and reproduce the version extant when you wrote down the results in your notes or paper. However, since it only takes one draw to mess things up, if you update R the results could change as well.

I realize that this is probably only problematic in rare cases, but I'm curious if there are any best practices here. This is something I've been struggling with in my own work.

If you want to step through code, perhaps rerunning blocks then you need a set.seed() call before each function call that will draw from the pseudo-random number generator.
• +1. Re the last paragraph: you don't have to save all that junk and you don't have to recreate an entire installation. If you're specific about which RNG you use, then rather than accepting defaults, all that needs saving are (1) the source code for that RNG (which is usually short) and (2) the state of the RNG at each crucial juncture. For most R work this state can found in .Random.seed. My biggest concern about R is that some routines might circumvent this--and perhaps could ignore set.seed altogether in some cases. – whuber May 14 '13 at 2:50