Fisher-Yates and List Randomization: Fair Permutations in Linear Time
Shuffling looks trivial until you need uniform randomness—biased shuffles skew simulations, game fairness, and A/B assignments. Fisher-Yates fixes that with a simple in-place loop and a trustworthy RNG behind it.
Walk the algorithm once by hand
For each index from the end down, swap with a random earlier index including itself. That single pass samples uniformly without extra memory—ideal for large arrays in memory-constrained environments.
Teams, dice, and layered randomness
Compare with team randomization and dice probability when teaching discrete probability—different tools, same call for honest randomness.
Fixtures and privacy
Shuffled test rosters should still follow synthetic data privacy rules—random order does not anonymize identities embedded in rows.
Partial shuffles and reservoir sampling
When you only need k random winners from n entries, reservoir algorithms beat full shuffles—less work and less memory on large streams.
Deterministic shuffles for tests
Seed PRNGs in unit tests to reproduce ordering bugs—document the seed in failure output so CI artifacts are replayable locally.
Weighted and stratified shuffles
Uniform randomization is not always desired—if strata must balance regions or skill levels, apply constraints before drawing randomness, as in team fairness workflows.
Recommended tools
Shuffle lines or items for quick experiments.
Partition shuffled lists into groups.
Supply indices for custom shuffle implementations.
Teach uniform discrete draws alongside shuffles.
Related guides
Frequently Asked Questions
What is Fisher-Yates shuffle?
An O(n) algorithm that produces a uniform random permutation—each ordering equally likely when the RNG is unbiased.
Why not sort by random keys?
Random comparator sorts are biased and inefficient—Fisher-Yates is the standard approach.
How do teams and dice relate?
Shuffle individuals first, then chunk into teams—or use dedicated team generators for balanced sizes.
Is synthetic data involved?
When test rosters stand in for real users, tag data and avoid leaking PII—same as other synthetic fixtures.
Can Fisher-Yates be biased by bad RNG?
Yes—modulo bias appears if you take `rand() % remaining` without rejection sampling; use unbiased integer ranges.
How do I shuffle immutable data structures?
Copy indices or use persistent vectors—functional languages often shuffle index arrays instead of mutating lists.
Wrapping up
Fisher-Yates is the right default for unbiased shuffles—pair it with a solid RNG and clear domain rules for teams and experiments.
List Randomizer →