A set of N distinct items can be arranged in N ! different ways. We wish to write a computer program that shuffles the items randomly. Here are two candidates:
Code 1
1 2 3 |
|
Code 2
1 2 3 |
|
Which of these codes, if any, will produce all N ! possible arrangements with equal probabilities?
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Neither of these methods is especially efficient either.
What would be a good order N method to shuffle?
Log in to reply
The last method is the most efficient. It makes N − 1 independent choices, making for N ⋅ ( N − 1 ) ⋅ ⋯ ⋅ 3 ⋅ 2 = N ! possible outcomes. For any permutation of N items, there is precisely one way in which the program can generate it.
For instance, in order to shuffle ( 1 , 2 , 3 , 4 , 5 ) into ( 3 , 1 , 4 , 5 , 2 ) , the program must
swap the first with the third item, to obtain ( 3 , 2 , 1 , 4 , 5 ) ;
swap the second item with the third, making ( 3 , 1 , 2 , 4 , 5 ) ;
swap the third item with the fourth, resulting in ( 3 , 1 , 4 , 2 , 5 ) ;
swap the fourth item with the last, resulting in ( 3 , 1 , 4 , 5 , 2 ) .
Log in to reply
Yes, I see now that you included this method. Thanks!
Suppose we input the alphabet: {'A', 'B', 'C', ..., 'Z'}.
Problem Loading...
Note Loading...
Set Loading...
Code 1 can produce every arrangement of the N items, but they do not have the same probability.
The program makes a certain number ( q ) of choices to swap or not to swap, so that there are 2 q equally likely runs of the code. The probability of finding a certain arrangement σ is therefore an integer multiple of 1 / 2 q . However, the desired probability is 1 / N ! . Since for N > 2 , the denominator N ! contains prime factors unequal to 2, this cannot be the desired distribution.
Consider for instance the case N = 3 . There are eight possible ways the code can run, but only six different arrangements. Working out the details, the eight possibilities are
Thus the arrangements 312 and 321 are twice more likely than the other four.
Code 2 is unable to produce every arrangement. At the beginning, the first element is swapped with some other element from the list. After that, there is no possibility for it to be swapped back to its original place. Therefore the code generates only (and all) the arrangements that do not start in 1.
It is not difficult to produce the correct code: