Shuffling cards so all orders are equally likely
Question
Given a generator of uniform random numbers, how do you shuffle a deck of cards so that each of the orderings has the same probability?
Short answer
The Fisher-Yates shuffle, time. For down to , pick uniformly from and swap cards and . The number of equally likely choice sequences is , one per permutation, so the result is uniform. Swapping each card with a position drawn from all is a common bug that biases the result.
Adapted from Zhou, A Practical Guide to Quantitative Finance Interviews, sec. 7.1 Algorithms, 'Random permutation A', p.175; question and answer rewritten by SelfMadeQuant.com.
See the hint and the full worked solution in the question bank (free account).
Open the worked solutionLessons behind this question
- Counting and Combinatorics
- Continuous Random Variables and Probability Density Functions
- Cumulative Distribution Functions
Start with a free lesson: Normal Distribution is part of a free topic. Create a free account, no card needed, to read it in full with worked solutions.
Read it free