Python Random setstate() 方法
实例
捕获并恢复随机数生成器的状态:import random
#print a random number:
print(random.random())
#capture the state:
state = random.getstate()
#print another random number:
print(random.random())
#restore the state:
random.setstate(state)
#and the next random number should be the same as when you captured the state:
print(random.random())
运行实例»
定义和用法
setstate()
方法用于将随机数发生器的状态恢复回指定状态
使用getstate()
方法捕获状态
语法
random.setstate(state)
参数值
参数 | 描述 |
---|---|
state | 必须。状态对象。setstate()方法将随机数生成器的状态恢复为此状态。 |