Probability is counting
The probability of an event is the number of ways it can happen divided by the number of equally likely outcomes. A fair die has six faces, so the chance of rolling a 4 is 1/6, about 16.7%. Programmers use this idea every time they write a random event: a 1-in-10 chance to drop a rare item, a 50% chance a coin lands heads.
var roll := randi_range(1, 6) # a whole number from 1 to 6
if randf() < 0.1: # randf() is a decimal from 0 to 1
print("Rare item! (10% chance)")Two dice are not like one
Roll two dice and add them. There are 6 × 6 = 36 equally likely pairs, but the sums are not equally likely. Only one pair makes 2 (1 and 1), but six pairs make 7: (1, 6), (2, 5), (3, 4), (4, 3), (5, 2), (6, 1). So 7 has probability 6/36 = 1/6, while 2 has only 1/36, under 3%. Board-game designers rely on this: the middle sums come up most often.
Roll two dice many times and compare the counts.
Small samples look lumpy. Large samples approach the 1-2-3-4-5-6-5-4-3-2-1 pattern.Average outcomes
The expected value is the long-run average. For one die it is (1 + 2 + 3 + 4 + 5 + 6) ÷ 6 = 3.5. No single roll is ever 3.5, but a thousand rolls will average close to it. Designers use expected value to balance games: if a chest gives 10 gold 90% of the time and 100 gold 10% of the time, its expected value is 0.9 × 10 + 0.1 × 100 = 19 gold.
Weighted choices
Loot tables often say “common 70%, rare 25%, legendary 5%.” One simple method: pick a random number from 1 to 100, then check which range it falls in. 1–70 is common, 71–95 is rare, 96–100 is legendary. The ranges have sizes 70, 25, and 5, matching the weights.
func pick_loot() -> String:
var r := randi_range(1, 100)
if r <= 70: return "common"
elif r <= 95: return "rare"
return "legendary"Shuffling fairly
A classroom picker such as Super Picker, a card game, or a quiz app needs to put items in a fair random order, where every arrangement is equally likely. The standard method is the Fisher–Yates shuffle: walk from the end of the list to the start, and swap each item with a randomly chosen item at or before it.
func shuffle_fair(items: Array) -> void:
for i in range(items.size() - 1, 0, -1):
var j := randi_range(0, i) # 0..i, including i
var temp = items[i]
items[i] = items[j]
items[j] = temp
# Godot's built-in items.shuffle() does this for you.The tempting version, “swap each item with any random position,” is subtly unfair. With 3 items it produces 3 × 3 × 3 = 27 equally likely sequences of swaps, but there are 6 possible orders. Since 27 cannot be split evenly into 6 groups, some orders must come up more often than others.
Computers fake randomness
Most program randomness is pseudo-random: a formula produces a long sequence of numbers that look random, starting from a number called a seed. The same seed always gives the same sequence. That is a feature: a game can share a seed so every player gets the same “random” daily level, and a programmer can replay a bug exactly.
var rng := RandomNumberGenerator.new()
rng.seed = 20261126 # same seed -> same sequence every time
print(rng.randi_range(1, 6))Probability, sample spaces, and expected value are part of middle-school statistics and Algebra II. A two-dice table is the same 6 × 6 grid you might draw to list outcomes. Simulating many rolls is how statisticians check a probability model, and it connects to geometric probability.
Where you will see it
- Games: dice, loot, critical hits, enemy behavior, and level generation.
- Classrooms: picking students or groups fairly.
- Science: Monte Carlo simulations estimate answers by repeating random trials.
- Testing: random inputs help find bugs a person would not think to try.
Exercises
With two dice, what is the probability the sum is 10?
Three pairs make 10: (4, 6), (5, 5), (6, 4). So 3/36 = 1/12.
A spinner pays 5 points with probability 0.8 and 50 points with probability 0.2. What is its expected value?
0.8 × 5 + 0.2 × 50 = 4 + 10 = 14 points.
Why might a daily puzzle use a seed based on the date?
Everyone gets the same puzzle that day, but a different one tomorrow, without storing every puzzle.
