How Random Number Generators Work in Your Browser
By RithyVerse Studio · Updated October 12, 2026 · 3 min read
A random number generator looks simple from the outside: you type a minimum, a maximum, press a button, and a number appears. Behind that button there are a few important decisions that decide whether the result is genuinely fair. This article walks through how a good browser-based generator works, using the approach behind Randomly as an example.
Step 1: get unpredictable bits
Browsers expose a secure source of randomness through the Web Crypto API. When a page calls crypto.getRandomValues(), the browser asks the operating system for bytes taken from its cryptographically secure generator. That generator is seeded with unpredictable events from your device, so the output cannot be guessed from earlier results.
This is different from the older Math.random() function. It is fine for decoration, such as shuffling a background animation, but it was not designed to resist prediction and the quality can vary between browsers.
Step 2: turn raw bits into a number in your range
The secure source gives you big random numbers, for example a value between 0 and 4,294,967,295. You almost never want that range. You want something like 1 to 100. The obvious trick is to take the remainder after dividing by the size of your range. Unfortunately, that quietly introduces a small unfairness known as modulo bias.
The problem with the remainder trick
Imagine you want a number from 0 to 5 (a six-sided die, numbered 0 to 5), and your source produces values from 0 to 4,294,967,295. That is 4,294,967,296 possible values, and six does not divide it evenly. The leftover is 4, so the first four outcomes each appear one extra time. The bias for a die is incredibly small, but it grows when the range is large relative to the source, and a truly fair tool should not have it at all.
The fix: rejection sampling
Rejection sampling avoids the bias by throwing away the values that would cause it. The generator finds the largest multiple of your range size that fits inside the source's range. If a random value falls inside that safe zone, it is used. If it falls in the leftover sliver at the top, it is discarded and a new value is drawn. Because the accepted values divide evenly into your range, every number, including both endpoints, has exactly the same chance.
The loop almost always finishes on the first try, so you never notice the retry, but the fairness guarantee is real.
Step 3: show the result
Once the number is chosen, the interface displays it. In Randomly the number is selected first and the animation only reveals it, so the wheel, crate or cards can never change the outcome. Skipping the animation gives the same kind of result, just faster.
Does it need a server?
No. Everything above happens on your own device. The range you enter and the numbers you draw are not uploaded anywhere, which makes the tool quick and private. It also means the generator keeps working if your connection is slow after the page has loaded.
What a fair generator should promise
- Every whole number in the range, including both endpoints, is equally likely.
- Past results do not influence future ones.
- The method is unpredictable to outsiders.
- Any visual effects do not alter the result.
If you want to read more about the differences between the types of randomness, see True Random vs Pseudo-Random Numbers, or go straight to the random number generator and try it.