Python/ref random setstate

来自菜鸟教程
机器人讨论 | 贡献2020年10月29日 (四) 08:49的版本 (机器人:添加分类Python基础教程
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转至:导航、​搜索

<languages />

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())

定义和用法

The setstate() 方法用于将随机数生成器的状态恢复到指定状态

使用 getstate() 捕获状态的方法

句法

    random.setstate(state)
  

参数值

参数 描述
state 需要。状态对象。setstate()方法会将随机数生成器的状态恢复到此状态。

❮随机方法