How can a resumed experiment continue at the same random draw?
getstate captures the current generator state. setstate can restore that position in a compatible environment.
Python example and interpretation
Save a checkpoint after consuming one draw. Restoring it reproduces the next draw. Reinitializing from the original seed starts the sequence over.
from random import Random
r=Random(6)
r.random()
state=r.getstate()
a=r.random()
r.setstate(state)
a==r.random()Result True
中文
跨断点续跑时,只保存随机种子足够吗?
getstate 保存伪随机发生器的当前内部状态,setstate 可在兼容环境中恢复到同一位置。
Python 算例与适用条件
先消耗一次抽样再存检查点,恢复后下一次抽样与原运行相同。仅从初始种子重建会回到序列开头。
from random import Random
r=Random(6)
r.random()
state=r.getstate()
a=r.random()
r.setstate(state)
a==r.random()结果 True