Weighted random choice: odds that favor one outcome
What is a weighted random choice? It is a random pick where the odds favor one outcome over another. Instead of every option having the same chance, each option gets a number called a weight, and a bigger weight means a better chance of being picked.
If you want to watch random choices happen on screen, Tank City Reboot is a free tank game that plays in your browser, on a computer or a phone. Its malware tanks choose where to go and its power-ups land on random spots, so you can see weighted choices at work while you play.

What is a weighted random choice?
Think of a bag of marbles. If the bag holds 6 red marbles and 1 blue marble, a blind pick gives you red far more often than blue. The marble count is the weight. Red has weight 6, blue has weight 1.
A weighted random choice does the same thing in code. You have a list of options, each with a weight. You run one random roll, and the weights decide which option the roll lands on. Common uses are loot drops, enemy types in a game, which ad to show, and which server gets the next request.
Weighted vs. uniform random: why the odds differ
A uniform random choice gives every option the same chance. Pick one of four enemy types uniformly and each shows up about one time in four. That is easy to write, and it is often the wrong tool.
Games and apps usually want some outcomes to be common and others rare. If a rare enemy appeared as often as the basic one, it would stop feeling rare. Weights let you set the feel directly. You decide that the basic enemy is common, the tough one is uncommon, and the tool does the rest.
Uniform random is just a weighted choice where every weight is the same. So weighted choice is the general case, and uniform is the special case.
How weights turn into probabilities
A weight is not a percentage. It is a share. To get the chance of one option, divide its weight by the sum of all the weights.
Say you have three options with weights 6, 3 and 1. The total is 10. The chances are:
- Weight 6 gives 6 out of 10, or 60%.
- Weight 3 gives 3 out of 10, or 30%.
- Weight 1 gives 1 out of 10, or 10%.
The weights do not have to add up to 100. Weights of 60, 30 and 10 give the same odds as 6, 3 and 1. Only the ratios matter. Use whatever numbers are easiest to read and change later.
A simple way to code it: the cumulative sum method
The most common method is easy to follow by hand. You stack the weights end to end on a number line and see where a random roll lands.
- Add up all the weights to get the total.
- Roll a random number from 0 up to, but not including, the total.
- Go through the options in order, keeping a running sum of their weights.
- Return the first option where the roll is less than the running sum.
Take weights of 60, 25, 10 and 5. The total is 100, and the running sums are 60, 85, 95 and 100. Suppose the roll is 72. It is not below 60, but it is below 85, so the second option wins. A roll of 3 would pick the first option. A roll of 99 would pick the last.
Each option owns a slice of the number line as wide as its weight. A random roll is equally likely to land at any point, so a wider slice is hit more often. That is the whole trick.
A worked example: loot drops and enemy spawns in a tank game
Here is an example with made-up numbers. They are not the numbers inside Tank City Reboot. Imagine a tank game with four malware types and this spawn table:
- Worm, weight 60
- Trojan, weight 25
- Spyware, weight 10
- Rootkit, weight 5
Over 20 spawns you would expect about 12 worms, 5 trojans, 2 spyware and 1 rootkit. Notice the word expect. A short run can drift. You might get no rootkit at all, or three. Over thousands of spawns the mix settles close to the weights.

Real games often use a mix of fixed and random rules. In the first Tank City Reboot game, each stage has a set lineup of 20 tanks, so the enemy order is not a pure dice roll. Stage 1 has 18 worms and 2 trojans, and stage 6 has 4 trojans, 6 spyware and 10 rootkits. The random part is elsewhere. The 4th, 11th and 18th tank of each stage blink and carry a power-up. When you hit a carrier, the game drops one of six power-ups at random, on a random spot. Enemy movement has a weighted side too. From the game's code, when a malware tank picks a direction it aims toward the CPU core half the time, toward you a quarter of the time and in a random direction a quarter of the time. Then it picks among the open directions with weight 4 for the aimed one and 1 for each of the others. That is a weighted random choice twice over: first for the target, then for the direction. Our enemy AI post follows the whole decision step by step.
In Tank City Zero Day, the second game, the table of options grows as the game goes on. Waves 1 and 2 send only botnet drones and worms. Ransomware can enter from wave 3 and the cryptominer from wave 4. This shows a useful pattern: the same roll gives harder results later because new rows join the table. For how those wave sizes are set, see the wave budget post.
You can build the same idea yourself. Keep a table per wave, and add a new row when you want a new enemy to appear. Start new rows with a small weight so the player meets it only now and then at first, then raise the weight as the player learns.
Using a seed to make weighted choices repeatable
A random roll comes from a random number generator, and a generator can be started from a seed. Start it from the same seed and you get the same rolls in the same order. That means the same weighted choices come out every time.
This is handy for testing. If a spawn table gives a strange result, you can replay it exactly. It also lets players share a run. For the full idea, read what a random seed is: same seed, same arena.
Zero Day uses this idea for its arenas. Every wave is a freshly generated 13 by 13 arena drawn from a seed, and it is a mirror image left to right. The game also checks that the arena is fair, meaning every enemy gate and your start can reach the core. If an arena is not fair, it is drawn again from the next seed. Rolling again until a result passes a check is a common companion to random choice.
Common mistakes with weighted random choice
Off by one at the edges
If your roll runs from 0 up to but not including the total, compare with "less than". If you compare with "less than or equal to", the first option gets one extra number and the last option may never be reachable. Test the first and last values by hand.
Weights of zero or less
A weight of zero means never. That can be useful, for example to turn an enemy off until a later wave. A negative weight breaks the running sum, so reject it. If every weight is zero, the total is zero and you have nothing to roll against, so guard against that case.
Thinking the dice are due
Each roll is independent. If a 5% option has not come up in 40 spawns, the next roll is still 5%. Players often feel the game is cheating. If you want to guarantee a drop after a long dry spell, add that as a separate rule, such as a counter that raises the weight.
Judging the odds from a tiny sample
Ten rolls tell you almost nothing. Test with thousands of rolls in a quick script and count the results. Then compare the counts with the shares you set.
Frequently asked questions
What is a weighted random choice in simple terms?
It is a random pick where each option has a weight that sets how likely it is. A weight of 6 beats a weight of 1, so that option comes up more often.
Do the weights have to add up to 100?
No. Only the ratios matter. Weights of 6, 3 and 1 give the same odds as 60, 30 and 10.
How do I work out the chance of one option?
Divide its weight by the total of all the weights. An option with weight 25 in a table that totals 100 has a 25% chance.
Can a weighted random choice be repeated exactly?
Yes, if you start the random number generator from the same seed. The same seed gives the same sequence of rolls, so the same choices come out in the same order.
Why does a rare option sometimes show up twice in a row?
Each roll is independent, so streaks happen. Over many rolls the mix gets close to the weights, but short runs can look uneven.
Get started with Tank City Reboot
The quickest way to feel random choice at work is to play with it. Watch which power-up drops from a blinking tank, and notice where the malware tanks head. You may spot the weights in what the game picks.
Play Tank City Reboot: it is free, plays in your browser on a computer or a phone, and needs no account.


Comments
No comments yet.
Sign in or make an account to comment.