难道这个简单的洗牌算法返回扑克牌随机洗牌的甲板?甲板、扑克牌、算法、简单

2023-09-11 04:34:25 作者:瞄了个咪

您有52张牌,其中的卡在该列表中的位置不移动的列表。 你有卡位置的第二个列表。首先,位置列表是相同的第一列表

You have a list of 52 cards where the position of the cards in that list does not move. You have a second list of card positions. At first, the position list is the same as the first list.

遍历第一个列表。

Iterate through the first list.

有关在第一个列表中的每个卡,产生第二个列表与卡在那个位置从1数到52。交换自己的立场。

For each card in the first list, generate a number from 1 to 52. Swap its position in the second list with the card in that position.

做了偏差存在吗?为什么呢?

Does a bias exist? Why?

更新:从来没有一个认为纯数学或逻辑,我决定实施这个自己。这里是第5卡(位置明智)是每个号码从1到52的%的机会:

Update: Never one to believe pure math or logic, I decided to implement this myself. Here are the percent chance of the 5th card (position-wise) to be each number from 1 to 52:

1. 1.9346%
2. 1.9011%
3. 1.8513%
4. 1.8634%
5. 1.8561%
6. 1.8382%
7. 2.5086%
8. 2.4528%
9. 2.4552%
10. 2.3772%
11. 2.3658%
12. 2.3264%
13. 2.3375%
14. 2.287%
15. 2.2627%
16. 2.2151%
17. 2.1846%
18. 2.1776%
19. 2.1441%
20. 2.1103%
21. 2.084%
22. 2.0505%
23. 2.0441%
24. 2.0201%
25. 1.972%
26. 1.9568%
27. 1.9477%
28. 1.9429%
29. 1.9094%
30. 1.8714%
31. 1.8463%
32. 1.8253%
33. 1.8308%
34. 1.8005%
35. 1.7633%
36. 1.7634%
37. 1.769%
38. 1.7269%
39. 1.705%
40. 1.6858%
41. 1.6657%
42. 1.6491%
43. 1.6403%
44. 1.6189%
45. 1.6204%
46. 1.5953%
47. 1.5872%
48. 1.5632%
49. 1.5402%
50. 1.5347%
51. 1.5191%
52. 1.5011%

正如你所看到的,完全是非随机的。我喜欢一个数学家证明为什么第五张是更可能是一个7比什么都重要,但我猜它,是因为有一个事实,即早期的卡,像7,有更多的机会来交换 - 这正是正确的算法prevents,只让卡换一次。

As you can see, quite un-random. I'd love a mathematician to prove why the 5th card is more likely to be a 7 than anything else, but I'm guessing it has to do with the fact that early cards, like 7, have more opportunities to swap -- which is exactly what the right algorithm prevents, it only lets cards swap once.

推荐答案

这是要误事的的费雪耶茨洗牌算法。请参阅What分配你从这个破碎的随机洗牌得到什么呢?,获取有关该执行性能进行了热烈讨论。

This is a common way to botch the Fisher-Yates shuffle algorithm. See What distribution do you get from this broken random shuffle? for a lively discussion on properties of this implementation.

这是如何不同于费雪耶茨?的

How is this different from Fisher-Yates?

有关费雪耶茨,在 K 次卡,你必须选择 K 和 52 。您选择的 1 随机数和 52 各一次。

For Fisher-Yates, at the kth card you must choose a random number between k and 52. You are choosing a random number between 1 and 52 each time.

您所描述的方法类似于What分配你从这个破碎的随机洗牌弄?,但可能不完全一样。你的实现是类似的还有研究上到随机洗牌(见Analysis顶到随机的洗牌的通过戴康尼斯,填充和皮特曼),最终将给出一个全面洗牌甲板上,虽然不是在一通。顶部的随机洗牌的描述如下:

The method you describe is similar to that discussed in What distribution do you get from this broken random shuffle?, but may not be exactly the same. Your implementation is similar to the well studies "Top to Random" shuffle (see Analysis of Top To Random Shuffles by Diaconis, Fill and Pitman) that will eventually give a fully shuffled deck, though not in "one pass". The Top to Random shuffle is described as following:

选择一个随机数 P 1 来    52 ,并交换了顶级显卡的   卡在位置 P 。继续,直到   顶牌原本是在卡   位置 52 ,并在此之后是   随机放置在甲板上是随机的   顺序。

Choose a random number p from 1 to 52, and swap the top card with the card at position p. Continue until the top card is the card originally in position 52, and after this is randomly placed the deck is in random order.

此停止条件就是所谓的停止时间,并且需要相当长一段时间才能实现。费雪耶茨洗牌远快。

This stop condition is known as a "Stopping Time", and takes quite a while to attain. The Fisher-Yates shuffle is far faster.