使用 Python random.shuffle 进行洗牌的最大列表长度? [英] Maximal Length of List to Shuffle with Python random.shuffle?

查看:28
本文介绍了使用 Python random.shuffle 进行洗牌的最大列表长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表,我使用 Python 内置的随机播放函数 (random.shuffle) 进行随机播放

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

但是,Python 参考说明:

However, the Python reference states:

注意,即使是很小的len(x),x的排列总数也大于大多数随机数生成器的周期;这意味着永远无法生成长序列的大多数排列.

Note that for even rather small len(x), the total number of permutations of x is larger than the period of most random number generators; this implies that most permutations of a long sequence can never be generated.

现在,我想知道这个相当小的 len(x)"是什么意思.100、1000、10000、...

Now, I wonder what this "rather small len(x)" means. 100, 1000, 10000,...

推荐答案

TL;DR:它在超过 2080 个元素的列表上中断",但不要太担心 :)

TL;DR: It "breaks" on lists with over 2080 elements, but don't worry too much :)

完整答案:

首先,请注意混洗"列表可以(概念上)理解为生成列表元素的所有可能排列,并随机选择这些排列之一.

First of all, notice that "shuffling" a list can be understood (conceptually) as generating all possible permutations of the elements of the lists, and picking one of these permutations at random.

然后,您必须记住,所有独立的计算机随机数生成器实际上都是伪"随机数.也就是说,它们实际上并不是随机的,而是依靠一系列因素来尝试生成一个难以预测或有意复制的数字.在这些因素中,通常是先前生成的数字.因此,在实践中,如果您连续使用随机生成器一定次数,您最终将再次开始获得相同的序列(这是文档中提到的周期").

Then, you must remember that all self-contained computerised random number generators are actually "pseudo" random. That is, they are not actually random, but rely on a series of factors to try and generate a number that is hard to be guessed in advanced, or purposefully reproduced. Among these factors is usually the previous generated number. So, in practice, if you use a random generator continuously a certain number of times, you'll eventually start getting the same sequence all over again (this is the "period" that the documentation refers to).

最后,Lib/random.py(随机模块)上的文档字符串说[随机数生成器的] 周期是 2**19937-1."

Finally, the docstring on Lib/random.py (the random module) says that "The period [of the random number generator] is 2**19937-1."

因此,考虑到所有这些,如果您的列表中有 2**19937 或更多排列,则其中一些将永远无法通过改组列表获得.您(再次,概念上)生成列表的所有排列,然后生成一个随机数 x,并选择第 x 个排列.下一次,您生成另一个随机数 y,并选择第 y 个排列.等等.但是,由于排列比你得到的随机数多(因为,最多在 2**19937-1 生成数字之后,你会再次开始得到相同的数字),你会再次开始选择相同的排列.

So, given all that, if your list is such that there are 2**19937 or more permutations, some of these will never be obtained by shuffling the list. You'd (again, conceptually) generate all permutations of the list, then generate a random number x, and pick the xth permutation. Next time, you generate another random number y, and pick the yth permutation. And so on. But, since there are more permutations than you'll get random numbers (because, at most after 2**19937-1 generated numbers, you'll start getting the same ones again), you'll start picking the same permutations again.

所以,你看,这并不完全是你的列表有多长的问题(尽管这确实进入了等式).另外,2**19937-1 是一个相当长的数字.但是,仍然,根据您的洗牌需要,您应该牢记所有这些.在简单的情况下(并通过快速计算),对于没有重复元素的列表,2081 个元素将产生 2081! 排列,这比 2**19937 多.

So, you see, it's not exactly a matter of how long your list is (though that does enter into the equation). Also, 2**19937-1 is quite a long number. But, still, depending on your shuffling needs, you should bear all that in mind. On a simplistic case (and with a quick calculation), for a list without repeated elements, 2081 elements would yield 2081! permutations, which is more than 2**19937.

这篇关于使用 Python random.shuffle 进行洗牌的最大列表长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆