你想看看random.getstate()
和random.setstate()
。将此与跟踪生成的项目数量相结合,做您想做的事情非常简单。
random
请注意,如果您依赖于此,则必须小心使用其他代码,因此我建议您创建一个random.Random()
实例来避免这种情况。
该模块提供的函数实际上是 random.Random 类的隐藏实例的绑定方法。您可以实例化您自己的 Random 实例以获取不共享状态的生成器。
(来自文档)
示例实现:
from itertools import islice
import collections
from random import Random
def consume(iterator, n):
"Advance the iterator n-steps ahead. If n is none, consume entirely."
# Use functions that consume iterators at C speed.
if n is None:
# feed the entire iterator into a zero-length deque
collections.deque(iterator, maxlen=0)
else:
# advance to the empty slice starting at position n
next(islice(iterator, n, n), None)
class RewindRandom:
def __init__(self):
self._random = Random()
self._state = self._random.getstate()
self._count = 0
def __iter__(self):
while True:
yield self._random.random()
def __call__(self):
self._count += 1
return next(iter(self))
def rollback(self, amount):
self._random.setstate(self._state)
consume(self, self._count-amount)
self._count -= amount
可以这样使用:
random = RewindRandom()
>>> random()
0.31276818768213244
>>> random()
0.7031210824422215
>>> random()
0.7196351574136909
>>> random.rollback(2)
>>> random()
0.7031210824422215
>>> random()
0.7196351574136909
>>> random()
0.6582894948982371