How to Build a Spin Wheel That's Actually Fair
Описание и применение
Разработчик создал инструмент WheelySpin, использующий криптографически безопасный генератор случайных чисел и метод отклонения для устранения модульного смещения, а также Fisher‑Yates для случайного перемешивания. Предварительный выбор победителя и последующая анимация гарантируют равные шансы для всех секторов колеса, что подтверждается тестом хи‑квадрат.
Задача
Spin wheels often appear rigged because of common JavaScript mistakes that introduce bias.
Результат
The wheel achieves statistically uniform odds, confirmed by chi‑square tests with p‑values around 0.69 for 100,000 spins.
Рабочий процесс
- Use crypto.getRandomValues() to generate a 32‑bit integer.
- Apply rejection sampling to remove modulo bias.
- Select winner with randomInt(entries.length).
- Shuffle entries with Fisher‑Yates using randomInt.
- Determine target rotation for the chosen slice.
- Animate wheel to that rotation.
- Run chi‑square test to verify fairness.
Доказательства
Math.random() is fine for confetti. It isn't designed for anything where someone might care about the result:
const winner = randomInt(entries.length); // the decision happens here
const limit = RANGE - (RANGE % n);
[20044, 19882, 19929, 20156, 19989] { chi2: 2.27, df: 4, p: 0.69 }
- Дата записи: 2026-10-17T08:56:05.649Z