在使用 seed
的时候,应该使用整数而不是浮点数。
树莓派上的 Python 3.5.3:
Python 3.5.3 (default, Sep 27 2018, 17:25:39)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import random
>>> random.seed(9248459222926972)
>>> [random.randint(1, 1000) for x in range(5)]
[586, 818, 989, 122, 519]
Python 3.7.3 在 Windows x64 上:
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import random
>>> random.seed(9248459222926972)
>>> [random.randint(1, 1000) for x in range(5)]
[586, 818, 989, 122, 519]
如果您正在使用可能在后台调用random.*()
的库代码,您可能还希望考虑仅在代码使用时实例化您自己的种子化RNG:
import random
rng = random.Random()
rng.seed(9248459222926972)
hash()
处理,并且默认情况下哈希值在每次解释器启动时都会随机化。 - AKX