python random.shuffle的随机性

发布于 2024-09-07 17:00:01 字数 399 浏览 1 评论 0原文

以下来自python网站,大约

random.shuffle(x[, random])

将序列x打乱到位。可选参数 random 是一个 0 参数函数,返回 [0.0, 1.0) 中的随机浮点数;默认情况下,这是函数random()

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

如果我想重复获得 ['a'..'k'] 的随机排列,似乎 shuffle 不会给我随机性。我的理解对吗?

谢谢你!

Following is from python website, about

random.shuffle(x[, 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().

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.

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?

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

累赘 2024-09-14 17:00:07

对于长度为 11 的序列,有 11 个!或 39,916,800 (~ 225.3) 种可能的排列。对于 Mersienne Twister(Python 的随机算法),周期为 219937 − 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文