Order bias in wrong implementation of Fisher Yates Shuffle

末鹿安然 提交于 2019-11-28 11:42:34

问题


I implemented the shuffling algorithm as:

import random
a = range(1, n+1) #a containing element from 1 to n
for i in range(n):
    j = random.randint(0, n-1)
    a[i], a[j] = a[j], a[i]

As this algorithm is biased. I just wanted to know for any n(n ≤ 17), is it possible to find that which permutation have the highest probablity of occuring and which permutation have least probablity out of all possible n! permutations. If yes then what is that permutation??

For example n=3:

a = [1,2,3]

There are 3^3 = 27 possible shuffle

No. occurence of different permutations:

1 2 3 = 4

3 1 2 = 4

3 2 1 = 4

1 3 2 = 5

2 1 3 = 5

2 3 1 = 5

P.S. I am not so good with maths.


回答1:


This is not a proof by any means, but you can quickly come up with the distribution of placement probabilities by running the biased algorithm a million times. It will look like this picture from wikipedia:

An unbiased distribution would have 14.3% in every field.

To get the most likely distribution, I think it's safe to just pick the highest percentage for each index. This means it's most likely that the entire array is moved down by one and the first element will become the last.

Edit: I ran some simulations and this result is most likely wrong. I'll leave this answer up until I can come up with something better.



来源:https://stackoverflow.com/questions/52272402/order-bias-in-wrong-implementation-of-fisher-yates-shuffle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!