Sub topic 11
Weighted Random Tables
Assume you know basic percentages, expected value, and common game mechanics such as dice and loot drops. This chapter shows how to turn those ideas into a weighted random table and how to avoid common design errors.
A weighted random table gives each outcome a weight. The weight is a relative share of the total, not always a percentage by itself. The game selects one outcome in proportion to its weight.
For example:
| Outcome | Weight | Probability |
|---|---|---|
| Common item | 70 | 70% |
| Rare item | 25 | 25% |
| Epic item | 5 | 5% |
The total weight is 100, so the weights also happen to be percentages. If the weights were 14, 5, and 1, the total would be 20. The probabilities would still be 70%, 25%, and 5%.
The basic formula is:
probability = outcome weight / total weight
Implementing a weighted table
Python's random.choices function selects from a sequence using relative weights. It selects with replacement, which means every draw starts with the same table.
import random
rng = random.Random(42)
table = {
"common": 70,
"rare": 25,
"epic": 5,
}
names = list(table)
weights = list(table.values())
drops = rng.choices(names, weights=weights, k=1000)
counts = {name: drops.count(name) for name in names}
print(counts)Across many draws, the counts should approach 700 common, 250 rare, and 50 epic items. They will not match those numbers exactly. Random variation is expected, especially in a small sample.
Design checks and common errors
A weighted table controls frequency, but it does not control the player's experience by itself.
- Check the total probability. Every valid outcome should be included, and the probabilities should sum to 100%.
- Separate selection from rewards. Selecting a rare item may still feel bad if the item is weak or duplicates too often.
- Decide whether draws are independent. Independent draws allow long streaks. A player can receive several rare items in a row or none for a long time.
- Consider conditional tables. A beginner area might use one table, while a late-game area uses another.
- Test small and large samples. A table can have the correct long-run rate but still produce frustrating short runs.
- Make weights readable. Store meaningful names and weights together so that later balance changes do not silently mismatch two separate lists.
A weight of zero is useful for disabling an outcome without removing it. Negative weights are invalid and should be rejected before selection.
Exercise
Exercise
A table has weights of 60, 30, and 10. What is the probability of selecting the second outcome, and what happens if every weight is doubled?
Show answer
The second outcome has probability 30 / 100 = 30%. Doubling every weight gives 120, 60, and 20, with a total of 200, so its probability remains 60 / 200 = 30%.