在 Python 中将长列表随机播放更长的次数

Shuffle a long list an even longer number of times in Python

我想多次打乱一个长序列(假设它有超过 10000 个元素)(比如 10000)。在阅读Python Random documentation时,我发现了以下内容:

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() 函数可能无法提供我需要的洗牌。

我考虑过一些解决方法:

我的替代方案有帮助吗?

非常感谢!

您可以使用 numpy shuffle function 就地随机排列列表元素

import numpy as np

L = range(0, 10000)
np.random.shuffle(L)

为 shuffle 调用计时(在 Jupyter 中)

%timeit np.random.shuffle(L)

你得到

10000 loops, best of 3: 182 µs per loop

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