简体   繁体   English

使用浮动种子 Python RNG 安全吗?

[英]Safe to seed Python RNG using float?

Can floating point values be passed as an argument to random.seed()?浮点值可以作为参数传递给 random.seed() 吗? Does this introduce unforeseen issues?这是否会带来不可预见的问题?

In other words.换句话说。 Is....是....

random.seed(0.99999999)
<use a few thousand random numbers>
random.seed(1)
<use a few thousand random numbers>

.... functionally equivalent to.... .... 功能上等同于....

random.seed(0)
<use a few thousand random numbers>
random.seed(1)
<use a few thousand random numbers>

Quick testing suggests that both sets of code run just fine and on a superficial level the outputs appear to be independent and deterministic.快速测试表明,两组代码都运行得很好,并且在表面上,输出似乎是独立的和确定性的。

I'm interested to know if this method of seeding is completely safe to use in cases where independence between seeded sets is important.我很想知道在种子集之间的独立性很重要的情况下,这种播种方法是否完全安全。 Obtaining deterministic results is also important.获得确定性的结果也很重要。 I've checked some of the documentation: Python 2.7 documentation and Python 3.8 documentation and done some googling and only found references to integers being used as a seed (or other data types which are converted to integers).我检查了一些文档: Python 2.7 文档Python 3.8 文档并做了一些谷歌搜索,只找到了对用作种子的整数(或转换为整数的其他数据类型)的引用。 I couldn't see any reference to floats and this makes me wonder if they are "safe" in the sense that they work in a predictable way with no nasty surprises.我看不到任何对浮动的引用,这让我想知道它们是否“安全”,因为它们以可预测的方式工作,没有令人讨厌的意外。

I'm currently working with Python 2.7 but am interested in the answer for more modern versions too.我目前正在使用 Python 2.7,但我也对更现代版本的答案感兴趣。

Using a float as a seed is intended functionality:使用浮点数作为种子是预期功能:

supported seed types are: None, int, float, str, bytes, and bytearray.支持的种子类型有:None、int、float、str、bytes 和 bytearray。

see: https://github.com/python/cpython/blob/master/Lib/random.py#L156见: https : //github.com/python/cpython/blob/master/Lib/random.py#L156

Getting a float of exactly the same value each time is critical for getting the same seed, but this is not too difficult.每次获得完全相同的浮点数对于获得相同的种子至关重要,但这并不难。 The most reliable way to always get the same float value is to not do any computation on it, or accept any user input.始终获得相同浮点值的最可靠方法是不对它进行任何计算,或接受任何用户输入。 If you want to ensure complete control, you can use struct.unpack to generate a float from raw binary data.如果要确保完全控制,可以使用struct.unpack从原始二进制数据生成浮点数。

Yes, it is safe to use a float seed是的,使用漂浮种子是安全的

According to the documentation , random.seed(a) uses a directly if it is an int or long , otherwise (if a is not None ) it uses hash(a) .根据文档,如果random.seed(a)intlong则直接使用a ,否则(如果a不是None )则使用hash(a) Given that python requires that hash(x) == hash(y) if x == y , this means that the same sequence of pseudo-random numbers will be generated for equal float seeds (with the standard caveats about strict comparisons of floating-point numbers).鉴于 python 要求hash(x) == hash(y) if x == y ,这意味着将为相等的浮点种子生成相同的伪随机数序列(关于严格比较浮点数的标准警告 -点数)。

The python 3 documentation is less clear about how it handles inputs of types other than int , str , bytes , and bytearray , but the behavior itself is the same as python 2 for python 3.8 and earlier. python 3 文档不太清楚它如何处理除intstrbytesbytearray之外的类型的输入,但行为本身与 python 3.8 及更早版本的 python 2 相同。 As was mentioned in Aaron's answer, seeding based on hashing is deprecated in 3.9, but float continues to be a supported seed type.正如 Aaron 的回答中提到的,基于散列的播种在 3.9 中已被弃用,但float仍然是受支持的种子类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM