如果先前生成的值明确设置为新种子,为什么 Python random.random() 会给出不同的值?

Why does the Python random.random() give a different value if the previously generated value is explicitly set as the new seed?

我读到 Python 中的 random 模块使用先前生成的值作为种子,除了第一次使用系统时间。 (, https://pynative.com/python-random-seed/) 如果这是真的,为什么当我像这样将先前生成的值显式设置为新种子时,我得不到相同的值:

random.seed(random.randint(1, 100))

同样不适用于 random.random() 方法。

>>> import random
>>> random.seed(20)
>>> random.randint(1,100)
93
>>> random.randint(1,100)
88
>>> random.seed(20)
>>> random.randint(1,100)
93
>>> random.randint(1,100)
88
>>> random.seed(20)
>>> random.seed(random.randint(1,100))
>>> random.randint(1,100)
64

为什么最后一个randint()调用没有给88?

提前致谢!

Python random 模块 不使用 先前生成的值作为种子。但是,Python 使用 Mersenne Twister generator to create pseudo-randomness. This algorithm is deterministic: that implies that it next state (the next generated number) depends on the previous one. This is different from the seed,这是一个用于配置生成器初始状态的值。

因为你读的是假的,或者你误解了它说的。 CPython 在幕后使用 Mersenne Twister 生成器,它的状态消耗 19937 位。您传递给 .seed() 的是 不是 新状态,而只是一堆位,通过未记录的(依赖于实现的)算法扩展为完整的 19937 位状态.

注意:如果您想保存和恢复状态,这就是 .getstate().setstate() 方法的用途。