RReal Randomizer
Sampling & Data3 min read•By Real Randomizer Editorial•Updated Nov 8, 2026

The Math of Shuffling & Unique Draws: Why Most Random Code Is Biased (Fisher-Yates vs Naive Sorting)

Understand non-repeating sampling, mathematical proof of why sort(() => Math.random() - 0.5) is broken, and how to draw unbiased raffle and lottery sets.

The Math of Shuffling & Unique Draws: Why Most Random Code Is Biased (Fisher-Yates vs Naive Sorting) featured illustration
Algorithm Test

Fisher-Yates vs. Naive Sort Bias

Shuffling 3 elements [A, B, C] has 6 possible permutations ($3! = 6$). Each should appear exactly 16.67% of the time. Watch how naive sorting produces severe mathematical skew because 27 outcomes cannot divide evenly into 6!

ABC16.7%
ACB16.7%
BAC16.7%
BCA16.7%
CAB16.7%
CBA16.7%
Quick Takeaway

Drawing random numbers without repeats guarantees that once an item is selected, it is permanently removed from the available pool. The common coding shortcut array.sort(() => Math.random() - 0.5) is mathematically broken because 27 possible permutation outcomes cannot divide evenly into 6 permutations. True unbiased shuffling requires the Fisher-Yates (Knuth) algorithm!

The Bucket Principle: Capacity Bounds

The simplest rule of non-repeating random selection is the Pigeonhole Principle: You cannot draw more unique items than exist in your source pool. If your range is numbers 1 through 10 (a pool of 10 integers), you cannot draw 12 unique numbers. A reliable tool like our Unique Random Number Generator immediately alerts you if your requested draw exceeds the pool capacity.

The Famous Developer Bug: Why Naive Sorting Is Biased

In countless web applications and codebases, developers shuffle an array using this one-line trick:

// WARNING: Broken algorithm! Do not use!
myArray.sort(() => Math.random() - 0.5);

While this code looks elegant, it is fundamentally mathematically biased. Here is the mathematical proof:

Mathematical Proof of Sorting Bias

Consider an array of 3 elements: [A, B, C]. The number of possible distinct permutations is 3! = 3 × 2 × 1 = 6 (ABC, ACB, BAC, BCA, CAB, CBA). For the shuffle to be fair, each permutation must appear with probability exactly 1/6 ≈ 16.67%.

However, when a comparison sorting algorithm compares 3 elements, each comparison chooses between two outcomes based on Math.random() - 0.5. A 3-element sort performs 3 comparisons, generating 2^3 = 8 branches, or 3^3 = 27 execution paths depending on sorting engine implementation.

Because 27 (or 8) is NOT divisible by 6, it is mathematically impossible for all 6 permutations to have equal probabilities!

Certain permutations (like BAC and ABC) receive significantly higher probabilities, while others (like CAB and BCA) are starved. Test this yourself in our live interactive simulator above!

The Gold Standard: Fisher-Yates (Knuth) Shuffle

To produce an unbiased shuffle, computer science uses the Fisher-Yates (Knuth) Shuffle algorithm. It runs in optimal linear time O(n):

function shuffle(array) {
  for (let i = array.length - 1; i > 0; i--) {
    // Pick an unbiased random index from 0 to i using CSPRNG
    const j = Math.floor(cryptoRandom() * (i + 1));
    // Swap elements array[i] and array[j]
    [array[i], array[j]] = [array[j], array[i]];
  }
  return array;
}

At each step i, the element placed into position i is chosen from exactly i + 1 candidates with equal probability 1 / (i + 1). The total probability of any permutation is:

(1/n) × (1/(n-1)) × ... × (1/1) = 1 / n!

Every single possible permutation has the exact same probability!

Ordered Sequences vs. Unordered Sets

Depending on your use case, make sure you choose the right output format:

  • Unordered Unique Sets: You only care which numbers were drawn (e.g. 5 winning lottery balls or 3 raffle ticket winners). Results are typically sorted numerically for easy scanning. Use our Unique Number Generator.
  • Ordered Permutations (Turn Orders): The specific sequence matters (e.g. race lane assignments, draft order, presentation speaker order). Use our Random Sequence Generator or Turn Order Generator.

The Birthday Paradox: Why Collisions Happen Fast

If you generate random numbers with replacement, duplicates occur far earlier than human intuition predicts. In a room of just 23 people, there is a greater than 50% probability that two people share the exact same birthday (out of 365 days)!

By 75 people, the probability of a shared birthday reaches 99.9%. This is why systems that require guaranteed uniqueness must explicitly draw from an urn without replacement or use 128-bit identifiers like our UUID v4 Generator (where collision probability is 1 in $5.3 imes 10^{36}$).

Related Topics:#unique numbers#without repeats#sampling
Instant Solution

Random Sample Generator

Put this math into practice right now. Safe, free, and runs 100% locally in your browser.

Open Free Tool →
Deepen Your Knowledge
View all Sampling & Data guides →