python random.shuffle的随机性 [英] python random.shuffle's randomness
问题描述
以下是来自python网站的关于
Following is from python website, about
random.shuffle(x[, random])
随机播放序列x
.可选参数random是一个0参数的函数,返回[0.0, 1.0)
中的一个随机浮点;默认情况下,这是功能random()
.
Shuffle the sequence x
in place. The optional argument random is a 0-argument function returning a random float in [0.0, 1.0)
; by default, this is the function random()
.
请注意,即使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.
如果我想重复获得['a'..'k']
的随机排列,似乎随机播放不会给我随机性.我的理解正确吗?
If I want to repeat getting a random permutation of ['a'..'k']
, it seems shuffle will NOT give me the randomness. Is my understanding right?
谢谢!
推荐答案
对于长度为11的序列,有11个!或39,916,800(〜2 25.3 )可能的排列.对于 Merienne Twister (Python的随机算法),周期为2 19937 − 1.换句话说,您会没事的.
For a sequence of length 11, there are 11! or 39,916,800 (~ 225.3) possible permutations. For the Mersienne Twister (Python's random algorithm) the period is 219937 − 1. In other words, you'll be fine.
这篇关于python random.shuffle的随机性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!