Python 随机模块 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() 方法将恢复;随机数生成器将返回到此状态。 |