Uniform Random Permutations and Shuffling

Prove Fisher–Yates uniform by looking at the remaining index range and account for bias in bounded random integers.

Source
Anbloq
Language
English
Created
Updated
Chapters
2
Reading time
1 min
In this article2 chapters
  1. Fill one position from the remaining items
  2. A modulo reduction can be biased

Fill one position from the remaining items

For an array of length \(n\), choose an index uniformly from \(0,\ldots,n-1\), swap its item into the last position, and recurse on the prefix. The chosen last item has probability \(1/n\). Conditional on that choice, the remaining positions are uniform by induction because the prefix contains exactly the other items. Each permutation therefore has probability \(1/n!\). Swapping every position with an index from the entire array generally has a different distribution.

A modulo reduction can be biased

Uniform integers from \(0\) to \(7\) reduced modulo three produce counts \(3,3,2\). Reject values \(6,7\), then take the remainder, so every class has the same number of surviving representatives. Choose a larger generator range for large indices. Uniform shuffling underlies some randomized selection implementations. Reservoir sampling produces a fixed-size uniform subset when the final stream length is unknown.[1][2]

References

  1. MIT OpenCourseWare, Design and Analysis of Algorithms: Lecture Notes ↩
  2. Michael Mitzenmacher and Eli Upfal, Probability and Computing ↩