python random.getstate() and random.setstate() python random.getstate() and random.setstate() python-3.x python-3.x

python random.getstate() and random.setstate()


Python's default generator is a Mersenne Twister with a state space that is 19937 bits, much larger than what you think of as the seed.

You can think of it conceptually as three functions:

  • f(seed) -> state0
  • g(statei) -> statei+1
  • h(statei) -> outcomei

When you start with a seed value using random.seed(), it generates a full state value of 19937 bits one time using function f(). Each time you use the generator, it advances to the next 19937 bit state using g() and returns the output found by collapsing the updated state down a single integer using h().

Normally you don't actually see the internal state which is at the core of the generator. getstate() bypasses the collapsing function h(), and setstate() bypasses the seeding function f(), so that you can reproduce your sequence from any point without having to go all the way back to the beginning and reproduce the entire sequence to that point.

Most people don't need to (and shouldn't) use the get/setstate capability, but it can be useful for pulling some clever mathematical tricks to reduce variability of Monte Carlo estimators.


Why not try it out?

import randomrandom.seed(42)print(random.sample(range(20),k=10))st = random.getstate()  # remeber this state print(random.sample(range(20),k=20)) # print 20random.setstate(st)     # restore stateprint(random.sample(range(20),k=10)) #print same first 10

Output:

[12, 0, 4, 3, 11, 10, 19, 1, 5, 18][4, 9, 0, 3, 10, 8, 16, 7, 18, 17, 14, 6, 2, 1, 5, 11, 15, 13, 19, 12][4, 9, 0, 3, 10, 8, 16, 7, 18, 17]

Obvoiusly, you can go back and reproduce the same values over and over if you get a state and restore it.

You can not use different randoms in between though or you alter the state.

random.setstate(st) # go back againprint(random.sample(range(99),k=2)) # do something differentprint(random.sample(range(20),k=18))

Output:

[21, 50]      # something different after setting state[0, 3, 11, 9, 18, 8, 17, 19, 16, 7, 15, 1, 10, 2, 12, 5, 13, 14] # changed values

import randomimport timeitt1 = timeit.timeit(stmt = """random.seed(42)random.randint(1,10)""",number=10000,setup="import random")t2 = timeit.timeit(stmt = """random.randint(1,10)random.setstate(s)""",number=10000,setup="""import randoms = random.getstate()""")print(t1,t2) 

Output:

# seed() time           setstate() time0.5621587821914207      0.49502014443357545