How to Build a Fair Spin Wheel: Why Math.random() Isn't Enough
If you've ever run a giveaway on a stream, picked a student in class, or settled "who presents first?" with an online spinner, someone has probably asked: was that actually random? While building WheelOfSpins, a free sp
If you've ever run a giveaway on a stream, picked a student in class, or settled "who presents first?" with an online spinner, someone has probably asked: was that actually random?
While building WheelOfSpins, a free spin wheel and random picker, that question shaped almost every technical decision. Here's how a fair spin wheel works under the hood, and the three mistakes that make many of them quietly unfair.
Mistake 1: Letting the animation decide the winner
The most common way to build a spinner is to pick a random rotation angle, animate the wheel, and then check which segment is under the pointer when it stops.
It looks random, but it causes problems:
- Rounding and easing curves can make some segments land slightly more often than others.
- Segments with long labels or odd sizes can skew the result.
- The result depends on frame timing, so it can differ between a fast phone and a slow laptop.
The fix: pick the winner first, then animate to it. Choose the result with a proper random function, then calculate the rotation that lands the pointer in the middle of that segment, plus a few full turns for drama. The spin only reveals a result that was already decided fairly.
const winnerIndex = pickRandomIndex(entries.length);
const segment = 360 / entries.length;
const target = 360 * 6 + (360 - (winnerIndex * segment + segment / 2));
wheel.style.transform = `rotate(${target}deg)`;
Mistake 2: Using Math.random() for things that matter
Math.random() is fine for confetti. But it isn't designed to be unpredictable, and browsers don't promise how it is implemented.
For a giveaway where real prizes are on the line, it's better to use the Web Crypto API, which every modern browser ships:
const buf = new Uint32Array(1);
crypto.getRandomValues(buf);
crypto.getRandomValues() draws from the operating system's secure random source, the same kind of randomness used for encryption keys. It runs locally, needs no server, and costs nothing.
Mistake 3: The modulo bias trap
Even with good random numbers, it's easy to break fairness with one line:
// Subtly biased!
const index = buf[0] % entries.length;
A 32-bit number has 4,294,967,296 possible values. Unless that divides evenly by your number of entries, some indexes get one extra chance. With small lists the bias is tiny, but it's real, and it's easy to avoid with rejection sampling: throw away the few values that would cause the imbalance and draw again.
function pickRandomIndex(n) {
const max = Math.floor(0x100000000 / n) * n; // largest multiple of n
const buf = new Uint32Array(1);
let x;
do {
crypto.getRandomValues(buf);
x = buf[0];
} while (x >= max);
return x % n;
}
Every entry now has exactly the same chance.
Bonus: Weighted entries without breaking fairness
Sometimes you want unequal odds, like giving someone with three raffle tickets three chances. The clean way is to treat each entry's weight as a number of slots and pick from the total:
function pickWeighted(entries) {
const total = entries.reduce((sum, e) => sum + e.weight, 0);
let r = pickRandomIndex(total);
for (const e of entries) {
if (r < e.weight) return e;
r -= e.weight;
}
}
The odds are exactly proportional to the weights, and you can show them openly.
Why this matters
Fairness isn't only about the math. People need to trust the result. A few things help:
- Show the result clearly so everyone in the room or on the stream sees the same thing.
- Offer "remove winner" so the same name can't win twice in a multi-prize draw.
- Keep it in the browser. When the pick happens on the user's device, nobody on a server can tamper with it.
Try it
You can see all of this in action at wheelofspins.com. It's free, needs no sign-up, and includes weighted entries, winner removal, custom colors and images, share links, and 10 languages. Teachers use it to pick students, hosts run giveaways, and teams decide who goes next.
If you're building your own picker, I hope the rejection-sampling snippet saves you a subtle bug. Questions or ideas? Leave a comment below.
Originally published on the WheelOfSpins Blog.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.