Maximal Length of List to Shuffle with Python random.shuffle?

后端 未结 3 1110
时光取名叫无心
时光取名叫无心 2020-11-29 06:12

I have a list which I shuffle with the Python built in shuffle function (random.shuffle)

However, the Python reference states:

No

3条回答
  •  执笔经年
    2020-11-29 06:48

    I wrote that comment in the Python source originally, so maybe I can clarify ;-)

    When the comment was introduced, Python's Wichmann-Hill generator had a much shorter period, and we couldn't even generate all the permutations of a deck of cards.

    The period is astronomically larger now, and 2080 is correct for the current upper bound. The docs could be beefed up to say more about that - but they'd get awfully tedious.

    There's a very simple explanation: A PRNG of period P has P possible starting states. The starting state wholly determines the permutation produced. Therefore a PRNG of period P cannot generate more than P distinct permutations (and that's an absolute upper bound - it may not be achieved). That's why comparing N! to P is the correct computation here. And, indeed:

    >>> math.factorial(2080) > 2**19937 - 1
    False
    >>> math.factorial(2081) > 2**19937 - 1
    True
    

提交回复
热议问题