<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[WheelOfSpins Blog]]></title><description><![CDATA[WheelOfSpins Blog]]></description><link>https://wheelofspins.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Sat, 28 Nov 2026 01:57:19 GMT</lastBuildDate><atom:link href="https://wheelofspins.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[How to Build a Fair Spin Wheel: Why Math.random() Isn't Enough]]></title><description><![CDATA[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…]]></description><link>https://wheelofspins.hashnode.dev/how-to-build-a-fair-spin-wheel-why-math-random-isn-t-enough</link><guid isPermaLink="true">https://wheelofspins.hashnode.dev/how-to-build-a-fair-spin-wheel-why-math-random-isn-t-enough</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[Tutorial]]></category><category><![CDATA[Beginner Developers]]></category><category><![CDATA[random]]></category><dc:creator><![CDATA[wheelofspins]]></dc:creator><pubDate>Thu, 26 Nov 2026 12:22:37 GMT</pubDate><content:encoded><![CDATA[<p>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: <em>was that actually random?</em></p>
<p>While building <a href="https://wheelofspins.com">WheelOfSpins</a>, 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.</p>
<h2>Mistake 1: Letting the animation decide the winner</h2>
<p>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.</p>
<p>It looks random, but it causes problems:</p>
<ul>
<li>Rounding and easing curves can make some segments land slightly more often than others.</li>
<li>Segments with long labels or odd sizes can skew the result.</li>
<li>The result depends on frame timing, so it can differ between a fast phone and a slow laptop.</li>
</ul>
<p><strong>The fix: pick the winner first, then animate to it.</strong> 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 <em>reveals</em> a result that was already decided fairly.</p>
<pre><code class="language-js">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)`;
</code></pre>
<h2>Mistake 2: Using Math.random() for things that matter</h2>
<p><code>Math.random()</code> is fine for confetti. But it isn't designed to be unpredictable, and browsers don't promise how it is implemented.</p>
<p>For a giveaway where real prizes are on the line, it's better to use the <strong>Web Crypto API</strong>, which every modern browser ships:</p>
<pre><code class="language-js">const buf = new Uint32Array(1);
crypto.getRandomValues(buf);
</code></pre>
<p><code>crypto.getRandomValues()</code> 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.</p>
<h2>Mistake 3: The modulo bias trap</h2>
<p>Even with good random numbers, it's easy to break fairness with one line:</p>
<pre><code class="language-js">// Subtly biased!
const index = buf[0] % entries.length;
</code></pre>
<p>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 <strong>rejection sampling</strong>: throw away the few values that would cause the imbalance and draw again.</p>
<pre><code class="language-js">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 &gt;= max);
  return x % n;
}
</code></pre>
<p>Every entry now has exactly the same chance.</p>
<h2>Bonus: Weighted entries without breaking fairness</h2>
<p>Sometimes you <em>want</em> 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:</p>
<pre><code class="language-js">function pickWeighted(entries) {
  const total = entries.reduce((sum, e) =&gt; sum + e.weight, 0);
  let r = pickRandomIndex(total);
  for (const e of entries) {
    if (r &lt; e.weight) return e;
    r -= e.weight;
  }
}
</code></pre>
<p>The odds are exactly proportional to the weights, and you can show them openly.</p>
<h2>Why this matters</h2>
<p>Fairness isn't only about the math. People need to <em>trust</em> the result. A few things help:</p>
<ul>
<li><strong>Show the result clearly</strong> so everyone in the room or on the stream sees the same thing.</li>
<li><strong>Offer "remove winner"</strong> so the same name can't win twice in a multi-prize draw.</li>
<li><strong>Keep it in the browser.</strong> When the pick happens on the user's device, nobody on a server can tamper with it.</li>
</ul>
<h2>Try it</h2>
<p>You can see all of this in action at <strong><a href="https://wheelofspins.com">wheelofspins.com</a></strong>. 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.</p>
<p>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.</p>
]]></content:encoded></item></channel></rss>