Why is seeding the random generator not stable between versions of Python?

后端 未结 2 1831
盖世英雄少女心
盖世英雄少女心 2020-12-16 10:40

I am trying to reproduce a random sequence from python\'s random.random() on a different system with a different python3 version installed.

This should

2条回答
  •  臣服心动
    2020-12-16 11:24

    The docs for seed say that they use the hash function to convert strings to valid input seeds. When I tested various versions of Python2.X (don't have 3 installed at the moment), some versions gave different values for hash(str(1)) Note that the docs for seed say that, regardless of version, they use the hash value for the string. You might want to pass an int instead (in addition to @pst 's point about using the backwards-compatible version of seed).

    Snippet from the random module docs for 3.2:

    If x is an int, it is used directly.

    With version 2 (the default), a str, bytes, or bytearray object gets converted to an int and all of its bits are used. With version 1, the hash() of x is used instead.

    (x here is the initializer for seed)

提交回复
热议问题