在Python中将长列表随机播放甚至更多次 [英] Shuffle a long list an even longer number of times in Python

查看:82
本文介绍了在Python中将长列表随机播放甚至更多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个长序列(比如说它有超过10000个元素)洗牌很多次(比如说10000个).在阅读 Python随机文档时,我发现了以下内容:

I want to shuffle a long sequence (say it is has more than 10000 elements)a lot of times (say 10000). When reading Python Random documentation, I found the following:

请注意,即使对于较小的len(x),x的排列总数也可以比大多数随机数生成器的周期迅速增大.这意味着长序列的大多数排列永远不会生成.例如,长度为2080的序列是可以在Mersenne Twister随机数生成器周期内找到的最大序列

Note that even for small len(x), the total number of permutations of x can quickly grow larger than the period of most random number generators. This implies that most permutations of a long sequence can never be generated. For example, a sequence of length 2080 is the largest that can fit within the period of the Mersenne Twister random number generator

我有两组(可能更多),每组都有很多值.我想打乱的顺序是所有可用值的列表,与组无关.我担心的是,该注释暗示我需要的随机播放可能不是由random.shuffle()函数提供的.

I have two groups (could be more) and each has many values. The sequence I want to shuffle is the list of all values available regardless of the group. My concern is that the note implies that the shuffle I need may not be provided by the random.shuffle() function.

我已经考虑过一些解决方法:

I have thought about some workarounds:

  • 在某些迭代中多次初始化随机数生成器(使用random.seed()).这样,排列是否大于周期都没有关系,因为不同的种子将获得不同的结果.
  • 使用样本(范围(序列的长度),k =组的大小)来获取随机索引,然后使用那些在每个组中进行索引.这样,由于随机数生成器的周期,我可能不会用完所有排列.

我的替代品有帮助吗?

非常感谢!

推荐答案

好吧10,000! ~= 10^36,000这是很多可能的排列.您可能要做的最好的事情是研究操作系统或硬件如何累积真正随机"的位.然后,您可以等待〜120,000位您可以接受的随机性,然后使用给定随机数n的输入列表生成第n个排列的算法.

Well 10,000! ~= 10^36,000 That is a lot of possible permutations. The best you could do is to delve into how your operating system or hardware accumulates "truly random" bits. You could then wait for ~120,000 bits of randomness that you are OK with then use the algorithm that generates the n'th permutation of your input list given that random n.

这篇关于在Python中将长列表随机播放甚至更多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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