What distribution do you get from this broken random shuffle?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
A correct random shuffle produces every permutation of a dataset with equal probability. But what happens when the shuffle algorithm has a bug? This article examines the most common "broken" shuffle, analyzes the non-uniform distribution it produces, and explains why the Fisher-Yates algorithm is the correct approach.
What Makes a Shuffle Correct?
For a dataset of elements, there are possible permutations. A correct shuffle gives each permutation exactly a probability of occurring. The Fisher-Yates (Knuth) shuffle achieves this by iterating from the end of the array to the beginning, swapping each element with a randomly chosen element from the remaining unprocessed portion.
This algorithm makes exactly swaps and produces each of the permutations with equal probability.
The Classic Broken Shuffle
The most common broken shuffle swaps each element with a random position chosen from the entire array, not just the unprocessed portion:
The subtle difference is that j ranges over all positions at every step, rather than only the positions from to .
Why This Produces a Biased Distribution
The broken shuffle makes independent random choices, each from options, producing equally likely execution paths. The key problem is that is generally not divisible by . For example, with :
- Number of execution paths:
- Number of permutations:
- , which is not an integer
Since 27 execution paths cannot be evenly distributed across 6 permutations, some permutations must be more likely than others.
Empirical Analysis for n = 3
Running the broken shuffle on [A, B, C] one million times produces results like:
| Permutation | Observed Frequency | Expected (Uniform) |
| [A, B, C] | 14.8% | 16.67% |
| [A, C, B] | 11.1% | 16.67% |
| [B, A, C] | 14.8% | 16.67% |
| [B, C, A] | 18.5% | 16.67% |
| [C, A, B] | 22.2% | 16.67% |
| [C, B, A] | 18.5% | 16.67% |
The distribution is clearly non-uniform. The permutation [C, A, B] appears about 33% more often than expected, while [A, C, B] appears about 33% less often.
Exact Probabilities
For the 3-element case, we can enumerate all 27 execution paths:
| Permutation | Paths leading to it | Probability |
| [A, B, C] | 4 | |
| [A, C, B] | 3 | |
| [B, A, C] | 4 | |
| [B, C, A] | 5 | |
| [C, A, B] | 6 | |
| [C, B, A] | 5 |
Statistical Detection
Chi-Squared Goodness-of-Fit Test
To statistically verify that a shuffle is biased, use the chi-squared test:
where is the observed count of permutation , is the expected count under a uniform distribution, and is the number of permutations.
With enough trials, a broken shuffle will produce a value far exceeding the critical value, confirming non-uniformity with high confidence.
Positional Bias Matrix
Another useful diagnostic is a position-frequency matrix. For each element, count how often it lands in each position across many trials. In a correct shuffle, each element should appear in each position with probability . The broken shuffle will show systematic biases, for example later elements tending to cluster toward the front.
Why Does This Matter?
Biased shuffling has real consequences in several domains:
- Online games: Card shuffles or matchmaking that favor certain outcomes create unfair play.
- A/B testing: Biased assignment of users to test groups invalidates experimental results.
- Cryptography: Non-uniform randomness weakens security protocols.
- Simulations: Monte Carlo simulations relying on uniform random permutations will produce incorrect results.
The Fix: Fisher-Yates
The Fisher-Yates algorithm avoids the bias by progressively narrowing the range from which the swap target is chosen:
This produces exactly equally likely execution paths, one for each permutation. It runs in time and extra space.
Summary
The broken shuffle (swapping with a random element from the full array) produces a non-uniform distribution because execution paths cannot map evenly onto permutations. The bias grows with array size. Always use the Fisher-Yates shuffle, which guarantees uniformity by restricting the swap range at each step. When in doubt, verify your shuffle with a chi-squared test on the permutation frequencies.

