01Why Randomness Is Hard for Computers
Computers are deterministic machines. That means that if you give a computer the same starting state and the same instructions, it always gives the same answer. So making something truly unpredictable inside software is a real challenge.
In the physical world, we use dice, coins and shuffled cards. Even these are not perfect: dice with drilled pips can be very slightly unbalanced, a coin toss can be affected by how it is flipped, and a quick hand shuffle may not mix the cards fully.
02PRNG vs. CSPRNG: The Key Difference
Computer random number generators fall into two main groups:
1. Pseudo-Random Number Generators (PRNG): 'Pseudo' means 'looks like but is not'. A PRNG uses a math formula that starts from a number called the seed and produces a long list of numbers that look random. JavaScript's Math.random() is one example; browsers use formulas such as xorshift128+. For a basic PRNG, someone who sees enough of its outputs can work out where it is in the list and predict the numbers that come next.
2. Cryptographically Secure Pseudo-Random Number Generators (CSPRNG): these are random generators built for security, so that past outputs cannot practically be used to predict future ones. In the browser, window.crypto.getRandomValues() gives you numbers from one. It is part of the W3C Web Cryptography API, the browser's built-in set of security tools. The operating system seeds it with entropy: hard-to-guess data collected from the computer, such as tiny differences in timing.
NIST Special Publication 800-90A describes approved designs for random bit generators. Each one takes a hard-to-guess seed from an entropy source and uses a fixed formula to turn it into a long stream of random-looking bits.
Source: NIST SP 800-90A Rev. 1: Recommendation for Random Number Generation (National Institute of Standards and Technology (NIST))03A Hidden Problem: Modulo Bias Explained
Even with a good random source, the way a number is turned into a winner can add a small, hidden favoritism called 'modulo bias'.
The modulo operator (written %) keeps only the remainder after division. Suppose you make a random byte between 0 and 255 (256 possible values) and pick a winner among 6 people with: winner = randomByte % 6.
Here is the problem: 256 does not divide evenly by 6 (256 = 42 x 6 + 4). The results 0, 1, 2 and 3 each come up 43 times, while 4 and 5 come up only 42 times. So the first four people have about a 16.8% chance of winning, and the last two have about 16.4%. Over many draws, the entries near the top of the list win slightly more often.
04How Rejection Sampling Keeps Chances Equal
Rejection sampling fixes modulo bias by throwing away the numbers that cause it. DrawLuma uses it in tools like the Random Number Generator, Dice Roller, Coin Flip Online, and Wheel of Names.
DrawLuma works with 32-bit numbers, which are whole numbers from 0 to 4,294,967,295. It finds the largest multiple of the number of entries that fits in that range. If a random number lands above that multiple, it is thrown away and a new one is used. Every number that is kept maps onto the entries evenly.
This gives every entry exactly the same chance of being picked. To see how equal chances behave in practice, read our guides on coin flip streaks and independence and dice probability distributions.
05Seeds, Entropy & SHA-256 Checksums
When you start a draw, DrawLuma asks the browser's secure generator for a fresh seed (a random starting number). It then turns that seed into the 32-bit numbers used above with SHA-256, a standard formula that always gives the same output for the same input. Because of this, the same seed, list and settings always give the same result, which is what lets a receipt be replayed.
After the draw, DrawLuma can save a receipt with the result, the entry list and settings, the seed, and the time from your device. It also adds a SHA-256 hash of the receipt: a 64-character code that works like a fingerprint of the file (explained further in our fair giveaway guide).
If even one character of the list or result changes, the hash changes completely, so it catches accidental edits. But anyone can work out a new hash after editing a file, so it is not a digital signature. It checks that the file is consistent. It cannot show who made the file, when the draw happened (the time comes from the organizer's device), or that no other draws were run. Running the draw live, or publishing the entry list before the draw, helps with those questions (see our About page).
Open Random Number Generator
Pick a random whole number in any range from -1,000,000 to 1,000,000. Every number has the same chance, and you can save a receipt to replay later.
Frequently Asked Questions
Why shouldn't I use Math.random() for raffles and giveaways?
JavaScript's Math.random() is not designed to be hard to predict, and browser documentation says it is not meant for security. For prize draws, a secure generator such as window.crypto.getRandomValues() is the safer choice.
What is uniform distribution?
A uniform distribution means every possible result has the same chance. For a draw with 10 people, each person has a 1 in 10 (10%) chance.
Can someone predict a DrawLuma winner in advance?
The winner is worked out from a fresh random seed that DrawLuma creates with the browser's secure window.crypto.getRandomValues() when you start the draw. Because that seed does not exist until the draw starts, the winner cannot be known before then.