๐ฒ Secure Random Integer Generator
Generates an unbiased, uniform random integer using the Web Crypto API and rejection sampling.
Generating...
Enter a minimum and maximum value to generate an unbiased, uniformly distributed random integer within that range, using your browser's Web Crypto API (a cryptographically secure random number generator). It avoids the "modulo bias" common with naive modulo-based random number generation by using rejection sampling.
How to use
- Enter a minimum and maximum value.
- The generated random integer is shown automatically.
- Click "Regenerate" to generate a new value.
How the calculation works
This tool picks one unbiased random integer from a range you choose (minimum to maximum, inclusive), using the browser's cryptographically secure random number generator (crypto.getRandomValues). It generates as many random bytes as needed to cover the range size, takes the remainder after dividing by the range size, and adds the minimum. Taking a plain remainder would make smaller numbers slightly more likely for some ranges (modulo bias), so random values at or above the largest multiple of the range size are discarded and redrawn (rejection sampling). Every number then has exactly the same probability. The arithmetic is done with BigInt (arbitrary-size integers), so large ranges are handled exactly.
Worked example
Range 1โ6 (a die) One random byte (0โ255) is used. 256 รท 6 = 42 remainder 4, so only values below 6 ร 42 = 252 are used; 252โ255 are redrawn. The result is (accepted value mod 6) + 1. Over 60,000 draws each face comes up about 10,000 times, an even distribution. The range can extend to the largest integers JavaScript represents exactly (ยฑ9,007,199,254,740,991).
Things to be aware of
- Useful for draws and raffles, parts of passwords, or random numbers in games.
- For a public draw, recording how the numbers were generated and the results makes it easier to show it was fair.
- Numbers are generated in your browser and never sent anywhere.
FAQ
What is modulo bias?
Taking a random byte value modulo the range size introduces a slight bias toward smaller values whenever the range isn't a power of two. This tool avoids that bias entirely using a technique called rejection sampling, producing a truly uniform distribution.
How is this different from Math.random()?
Math.random() is a pseudo-random number generator with no cryptographic security guarantee. This tool uses the Web Crypto API (crypto.getRandomValues), making it better suited for use cases involving drawings/lotteries or security.
Is the generated value sent anywhere?
No. All processing happens entirely in your browser โ the generated value is never sent to a server.