# Web Crypto vs Math.random(): Fair Draws in the Browser

Every giveaway picker on the internet claims to be fair. Most of them are one line of Math.random() wrapped in a nice UI. The uncomfortable question is whether that line can support the claim — and the answer depends on what "fair" has to mean once a prize is on the line.

I built a free [Random Number Generator](https://toolkitloop.com/random-number-generator) that draws from the browser's Web Crypto API instead. This post walks through the three engineering decisions behind it: the random source, the bias fix, and the draw modes that decide who can win twice.

## The random source: Web Crypto, not Math.random()

Math.random() returns a pseudorandom double from an implementation-defined algorithm. The specification does not fix which algorithm a browser uses, and it is explicitly not designed for security-sensitive use. For shuffling a playlist, that is fine. For a draw, it leaves the host making a promise — "nobody could predict the winner" — that the generator was never designed to keep.

crypto.getRandomValues() is the browser's cryptographic source. It draws from the operating system's entropy pool, and it is the same primitive behind key generation and secure tokens. Using it for a number draw is not over-engineering; it is using the right tool for a job where unpredictability is the entire point.

## Rejection sampling kills modulo bias

The second decision is subtler, and it is the one most implementations get wrong. The common pattern maps a large random value into a smaller range with a remainder:

```javascript
// Biased: the range rarely divides the source space evenly.
const biased = min + (randomUint32 % range);
```

When the range does not divide the source space evenly, the low end of the range gets picked slightly more often. The skew is small — far too small to notice in a hundred draws — but fairness is a binary claim. Either every integer has exactly the same probability, or the claim "every entry had an equal chance" is false in the fine print.

Rejection sampling fixes it by throwing away out-of-range values instead of wrapping them:

```javascript
function secureRandomInt(min, max) {
  const range = max - min + 1;
  const maxUint32 = 0xffffffff;
  const limit = Math.floor((maxUint32 + 1) / range) * range - 1;
  const buf = new Uint32Array(1);
  let value;
  do {
    crypto.getRandomValues(buf);
    value = buf[0];
  } while (value > limit); // reject, don't wrap
  return min + (value % range);
}
```

The retry loop looks wasteful until you do the arithmetic: the rejection probability is at most range / 2^32, negligible for any realistic range. You pay a rare extra draw and buy exact uniformity.

## Intervals, not arrays: why huge ranges still work

A naive implementation builds the list of allowed values and picks from it. That works for 1–100 and dies at 1–1,000,000,000. The generator instead represents allowed values as intervals — the full range minus the excluded numbers and ranges — and samples inside those intervals. Exclusions (a disqualified entry, an already-called bingo number, a taken seat) are interval arithmetic, not a billion-element array. The practical consequence: you can exclude whole ranges before a draw and the page stays responsive.

## No repeats vs elimination mode

The mode design is where most giveaway hosts make their one real mistake. The two modes answer different questions:

- **No repeats** guarantees uniqueness *within one multi-number result*. Ten numbers from 1 to 100 come back all different. It does not reserve anything: the next draw is a fresh draw, and a value from the first result can appear again.
- **Elimination mode** draws one number at a time and removes each earlier draw from the current pool until you reset it. Third place, second place, first place — nobody wins twice.

If you run a giveaway as a sequence of separate draws, only elimination mode gives you the "no repeat winners" property your entrants assume you have. The tool also enforces the mathematical ceiling honestly: the maximum number of unique values equals the size of the range (100 unique values from 1–100). Ask for more and it asks you to expand the range instead of silently repeating numbers.

## Sorting, presets, and the boring parts that matter

Small details decide whether a tool gets used twice. Presets (1–100, 1–10, and similar) remove the setup friction. Sorting the result turns a draw into a score sheet for bingo or a classroom list. Copying the result in one click matters when the draw is being pasted into a live chat. None of these is technically interesting; all of them are the difference between a tool and a demo.

## The honest limit

The tool page states its limit in plain language, and it is worth repeating: this is a neutral browser-side selection aid, not an independently audited or legally verifiable draw service. A screen-recorded Web Crypto draw is a strong, checkable story for a classroom, a community giveaway, or a Discord raffle. A regulated lottery needs an audited service with a verifiable trail. Part of building a fair draw is knowing which side of that line you are on — and saying so to your entrants.

## Try it

The [Random Number Generator](https://toolkitloop.com/random-number-generator) is free, needs no signup, and runs entirely in your browser: your range, exclusions, and results are processed in the current session and never sent anywhere. Draw your next winner in Elimination mode, record the screen, and link the method alongside the result. Transparency is the digital version of letting everyone watch the hat.

*Disclosure: I built this tool. It runs entirely in your browser, needs no signup, and is free to use.*
