简体   繁体   English

Python种子数

[英]Python number of rng seeds

In the docs (python 3.5) for initializing a random number from a seed: 在用于从种子初始化随机数的文档(python 3.5)中:

random.seed(a=None, version=2) random.seed(a =无,版本= 2)

Initialize the random number generator. 初始化随机数生成器。

If a is omitted or None, the current system time is used. 如果省略a或无,则使用当前系统时间。 If randomness sources are provided by the operating system, they are used instead of the system time (see the os.urandom() function for details on availability). 如果操作系统提供了随机性源,那么将使用它们而不是系统时间(有关可用性的详细信息,请参见os.urandom()函数)。

If a is an int, it is used directly. 如果a是一个int,则直接使用它。

With version 2 (the default), a str, bytes, or bytearray object gets converted to an int and all of its bits are used. 使用版本2(默认值)时,str,bytes或bytearray对象将转换为int并使用其所有位。 With version 1, the hash() of a is used instead. 在版本1中,使用a的hash()代替。

It does not make it clear how many seeds there are. 尚不清楚有多少种子。 An int has normally only 4 billion distinct values, but pythons include arbitrary precision: 一个int通常只有40亿个不同的值,但是python包含任意精度:

x = 1
type(x) # <class 'int'>
y = 123456789123456789123456789123456789123456789123456789123456789123456789
type(y) # <class 'int'>
z = x+y
z-y # 1 (no rounding error for a 71 digit number)

They say all of its bits are used but that could mean that the bits are used to make a digest that is a normal 32 bit int. 他们说所有的位都被使用了,但这可能意味着这些位被用来做一个通常为32位int的摘要。 Why does this matter? 为什么这么重要? I need a make random patterns from seeds. 我需要从种子制作随机图案。 In turn, I need to makes random sequences of patterns (the sequence in turn has a seed). 反过来,我需要制作随机的图案序列(该序列又有种子)。 A stream of random number generators will be subject to a "birthday attack" in which after a 100 thousand or so there is almost certainly going to be a duplicate if it's only 32 bits. 一串随机数生成器将受到“生日攻击”,在这种情况下,大约只有十万位左右,如果经过十万左右,几乎肯定会重复。 Although this is not for cryptography, it's still undesirable. 尽管这不是用于密码学的,但这仍然是不可取的。

What's great about open source is the ability to simply view the code with a question. 开源的最大好处是能够简单地查看带有问题的代码。 This is the source of random.seed : 这是random.seed来源

if a is None:
    try:
        # Seed with enough bytes to span the 19937 bit
        # state space for the Mersenne Twister
        a = int.from_bytes(_urandom(2500), 'big')
    except NotImplementedError:
        import time
        a = int(time.time() * 256) # use fractional seconds

if version == 2:
    if isinstance(a, (str, bytes, bytearray)):
        if isinstance(a, str):
            a = a.encode()
        a += _sha512(a).digest()
        a = int.from_bytes(a, 'big')

super().seed(a)
self.gauss_next = None

You can see that if version == 2 and str / bytes are provided, it takes the SHA512 of a , appends it, and uses int.from_bytes , generating a very large int and guaranteeing at least a 512-bit seed, even with very small custom inputs. 你可以看到,如果version == 2str / bytes被提供,它需要的SHA512 a ,追加它,并且使用int.from_bytes ,产生一个非常大的int和至少保证一个512位的种子,甚至具有非常小自定义输入。

As noted below, the end result is that the seed is guaranteed to have a length of at least 624 bits. 如下所述,最终结果是保证种子的长度至少为624位。

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

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