How to Build a Spin Wheel That's Actually Fair
说明与用途
开发者构建了 WheelySpin,使用 crypto.getRandomValues() 与拒绝采样消除模数偏差,并采用 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