Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
864 views
in Technique[技术] by (71.8m points)

python - Order bias in wrong implementation of Fisher Yates Shuffle

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

permute with all order bias

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...