Seeding Python's built-in (pseudo) random number generator will allows us to get the same response each time we use that seed -- documentation here. And I've heard saving the internal state of the generator reduces the likelihood of repeating values from previous inputs. Is this necessary? That is, is getState()
and setState()
in the below code unnecessary in order to get the same results every time I seed with "foo"
?
import random
...
state = random.getstate()
random.seed("foo")
bitmap = random.sample(xrange(100), 10)
random.setstate(state)
return bitmap
No, setting either the seed or the state is sufficient:
Setting the seed is usually more convenient to do programatically than setting the RNG state explicitly.