Weighted Random: How Weighted Random Selection Works
Weights change probabilities, not randomness itself. Learn cumulative weights, inverse transform sampling, and how to verify a fair weighted draw.
Weighted random selection is what you use when options should not have equal probability. Weights tell you how often each option should win, on average.
Key takeaways
- Probability = weight divided by the total weight.
- Cumulative weights turn options into segments of the interval from 0 to 1.
- A secure random value picks a point; the matching segment wins.
- Weighted without replacement removes the winner and renormalizes the remaining weights.
- Test fairness by comparing observed and expected frequencies over many draws.
Equal versus weighted randomness
An ordinary random picker treats every option equally. That is correct for a classroom draw where each student should have the same chance. But many real decisions are deliberately unequal:
- A game wants common loot to appear often and rare loot to appear rarely.
- A raffle gives some participants more tickets than others.
- An A/B test sends 80% of users to the control variant and 20% to the new one.
- A teacher wants hard questions to appear more often than easy ones.
Weighted randomness is still randomness. The difference is that the probability distribution is intentional.
Weights are probabilities, not counts
The weight of an option is not itself a probability. It is a relative number. To get a probability, divide the weight by the sum of all weights.
Suppose the options are:
- A, weight 1.
- B, weight 2.
- C, weight 7.
The total weight is 10, so the probabilities are 10%, 20%, and 70%. If you draw 100 times, C should win about 70 times. If you draw only 10 times, C might win 4, 6, or even 0 times. Small samples are noisy; the weights describe the long-run expectation.
The cumulative distribution
The cumulative distribution turns those weights into segments of the interval from 0 to 1. With the same example:
- A covers 0.00 to 0.10.
- B covers 0.10 to 0.30.
- C covers 0.30 to 1.00.
Each segment's width is the option's probability. The segments do not overlap, and together they cover the entire interval.
This is why weighted selection does not need a separate probability for every option. You only need the weights and their total.
Inverse transform sampling
Inverse transform sampling is the standard way to select from that distribution:
- Calculate the total weight.
- Pick a random value between 0 and the total.
- Walk through the options, adding each weight to a running total.
- Stop at the first option whose cumulative weight is greater than or equal to the random value.
The selected option is the winner. If the random value lands in A's segment, A wins. If it lands in C's segment, C wins.
The algorithm takes O(n) time per draw, where n is the number of options. It is simple, correct, and more than fast enough for a page with a few dozen options.
With and without replacement
With replacement, each draw is independent. The same option can win more than once. This is the right model when every draw should use the original probabilities.
Without replacement, a selected option is removed from the pool. The remaining weights are renormalized before the next draw. If A is removed, the remaining total is smaller, so B and C get larger shares of the next draw.
Without replacement is not a shuffle. It does not produce every permutation with equal probability. It produces a weighted order: options with larger weights tend to appear earlier.
The alias method for high-volume picks
For a huge number of draws or very large option sets, there is a faster alternative called the alias method. It builds a lookup table once and then answers each draw in constant time.
The alias method is useful in games and simulations that need millions of picks. For a browser tool with a small list, cumulative inverse transform sampling is simpler and perfectly adequate.
Common mistakes
Weights are easy to get wrong. Watch for these mistakes:
- Treating a weight of 70 as 70% when the other weights are not zero. It is only 70% if the other weights sum to 30 or less.
- Allowing zero or negative weights. Zero-weight options can never win. Negative weights make the distribution meaningless.
- Using Math.random() for a draw that needs to be unpredictable.
- Using a biased mapping to turn random values into segments.
- Judging fairness from ten or twenty draws. Small samples can look very different from the expected distribution.
Another common mistake is confusing weighting with shuffling. If you need every permutation equally likely, shuffle the list. If you need a weighted first pick, use a weighted draw.
How to verify a weighted draw
Compare expected and observed frequencies. With weights 1, 2, and 7, the expected probabilities are 10%, 20%, and 70%. Over thousands of draws, the observed proportions should approach those numbers.
Do not overinterpret a small sample. A fair weighted draw can produce a surprising streak. What matters is whether the observed frequencies converge as the sample grows and whether the implementation is documented.
For a public weighted draw, record the options, weights, and result. That makes the draw auditable even if you never publish the code.
How this site implements weighted random
The weighted picker on this site uses cumulative weights and a cryptographically secure random value. The probability preview shows the normalized distribution before you draw. Options are selected with inverse transform sampling, and without-replacement draws remove the selected option and renormalize the remaining weights.
Everything runs locally in your browser. Your options, weights, and results are not uploaded.
Bottom line
Weighted randomness changes probabilities, not the source of randomness. Normalize the weights, use a secure random value, and select the matching cumulative segment. That gives you fair unequal probabilities for loot tables, raffles, A/B tests, and classroom activities.
Try the tool
Weighted Random Picker
Pick items with custom probabilities.