April 12, 2026
Shuffle or sorcery?
Floyd's Sampling Algorithm
‘Magical’ number-picker has coders swooning — and sparring — while remembering Floyd
TLDR: A short, elegant trick for picking random items fairly—Floyd’s algorithm—sparked a split between “it’s pure magic” fans and “it’s just the tail end of a shuffle” realists, while a top comment turned the thread into a tribute to Robert Floyd. It’s a reminder that tiny ideas can be both clever and lasting.
The nerd-sphere is buzzing over a tiny snippet of code that picks a handful of numbers from a list perfectly fairly. Fans call Floyd’s sampling algorithm “magic,” pointing to that cheeky little branch—“if your number’s already taken, take mine”—that feels like a logic plot twist. Others clap back: it’s not sorcery, it’s just the last few moves of a classic shuffle, the Fisher–Yates mix-up you use to randomize a deck. Cue the split: Team Magic vs Team It’s-Just-a-Shuffle.
In plain English: instead of shuffling the whole deck, this trick only does the final few swaps and still gets an honest, random sample. The post lays out two simple explanations—one about how each step keeps things fair, and another tying it directly to the Fisher–Yates shuffle. Pragmatists cheer the elegance; purists demand intuition that doesn’t feel like a math proof. Memes fly: “numbers playing musical chairs,” “if you take my seat, I’m taking yours,” and “I shuffled just the tail and all I got was this perfect subset.”
Then the tone shifts. A top comment notes that computer science legend Robert Floyd died at 65 of a degenerative brain disorder, and the thread turns into a mini-tribute. Between the jokes and debates, people pause to salute the mind behind the magic, mixing awe, grief, and a renewed appreciation for beautifully compact ideas.
Key Points
- •The article presents Robert Floyd’s algorithm to sample k distinct integers uniformly from 1..n.
- •Implementation iterates i from n−k+1 to n, selects t uniformly in [1..i], and inserts t unless already chosen, otherwise inserts i.
- •A proof intuition shows each step maps k-subsets plus a draw to (k+1)-subsets in a (k+1)-to-1 fashion, ensuring uniformity.
- •The algorithm is linked to the Fisher–Yates shuffle, showing it corresponds to performing the final k swaps of the shuffle.
- •Taking the shuffled array’s last k elements yields the same uniform subset as Floyd’s method, up to ordering.