使用Python random.shuffle随机播放的列表的最大长度? [英] Maximal Length of List to Shuffle with Python random.shuffle?

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

问题描述

我有一个列表,可以使用内置的随机播放功能(random.shuffle)的Python随机播放

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,并选择yth排列.等等.但是,由于排列的数量多于获得随机数的数量(因为最多在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天全站免登陆