On the far bank of the ford a bridge guard bars Ada's way, and this time talk
will not do. Ada carries two weapons: a light sword she handles well, and a
heavy axe that is harder to land but hurts more. Which should she swing? Each
attack is uncertain twice over: it may miss, and if it hits, a die decides how
much harm it does. This lesson turns each choice into one number, its
expected valueExpected valueThe average result of a choice if you made it many times: each outcome's value times its chance, added up., and lets the numbers choose.
An attack, in two steps
An attack is a check followed by a roll:
Does it hit? Roll the d20, add the weapon's
modifierModifier (role-playing)A number added to a die roll for one kind of attempt: positive for something a character does well, negative for something they do badly., and compare with the
guard's defence, a difficulty of 14.
How much does it hurt? If it hits, roll the weapon's damage dice. The
result is the attack's damage: how much harm it does. A miss does none.
Weapon
To hit
The die must show
Chance to hit
Damage
Sword
d20 + 5
9 or more
12 faces: 60 %
1d8 + 3
Axe
d20 + 3
11 or more
10 faces: 50 %
2d6 + 3
The sword hits more often; the axe hits harder. Neither fact alone settles it.
The average of a roll
A fair die with s faces averages (1 + s) ÷ 2, the middle of its range: a
d6 averages 3.5 and a d8 averages 4.5. Dice that are added have averages that
add too: 2d6 averages 3.5 + 3.5 = 7, the peak of the triangle in “One die, two dice: the distribution of a roll”. So a
sword hit averages 4.5 + 3 = 7.5 damage, and an axe hit 7 + 3 = 10.
Expected value
The expected value of a choice is the
average result you would get if you made the same choice many times. To
compute it, list the outcomes, multiply each one's value by its chance, and
add the products:
E=p1v1+p2v2+⋯+pnvn
where p1 is the first outcome's chance and v1 its value, p2 and v2
the second's, and so on for all n outcomes.
For the sword, the outcomes are a miss (chance 0.4, damage 0) and a hit (chance
0.6, averaging 7.5):
Esword=0.4×0+0.6×7.5=4.5Eaxe=0.5×0+0.5×10=5
The axe deals 5 damage per swing on average, the sword 4.5. Over a long
fight, the axe is the better choice, even though it misses more often.
ⓘ Concept: An average, not a promise
No single sword attack deals 4.5: it deals 0 when it misses, or something
from 4 to 11 when it hits. Expected value describes what happens over many
attacks: swing the sword a hundred times and the damage adds up to about 450.
It is the right way to compare choices that are made again and again. The
next lesson, “Beyond the average: risk, variance and ruin”, shows when one bad result matters more
than the average.
Why it matters — Expected value compares choices; it does not predict the next roll.
The same tool scores choices in board games too: a program that cannot see to
the end of a game can play it out at random many times from a move, and score
the move by its average result. A later pillar of Game Boards, Minimax, Pruning
& Evaluation, builds such a program.
The check, twice
The program below computes each weapon's expected damage exactly, by going
through every face of the d20 and every way the damage dice can land, and then
by simulating ten thousand attacks with a fixed seed.
import random
from itertools import product
def attack_exact(to_hit, dice, sides, plus, defence=14):
"""Expected damage of one attack: chance to hit times the average hit."""
rolls = list(product(range(1, sides + 1), repeat=dice))
average_hit = sum(sum(r) + plus for r in rolls) / len(rolls)
faces_hitting = sum(1 for face in range(1, 21) if face + to_hit >= defence)
return faces_hitting / 20 * average_hit
def attack_simulated(to_hit, dice, sides, plus, defence=14, attacks=10_000, seed=1):
"""Damage per attack, averaged over many simulated attacks."""
roller = random.Random(seed)
dealt = 0
for _swing in range(attacks):
if roller.randint(1, 20) + to_hit >= defence:
dealt += sum(roller.randint(1, sides) for _die in range(dice)) + plus
return dealt / attacks
print(attack_exact(5, 1, 8, 3), round(attack_simulated(5, 1, 8, 3), 2)) # 4.5 4.47
print(attack_exact(3, 2, 6, 3), round(attack_simulated(3, 2, 6, 3), 2)) # 5.0 4.95
Each simulated average lands within 0.1 of its exact value, and the axe stays
ahead.
Your turn
The first rung asks for one number: what a d8 rolls on average. The second,
written from scratch, computes an expected value from a list of outcomes and
uses it to compare the two weapons. Then a short gapped set checks the idea.
Ask the mentor about this module
Ask a question about this content. The mentor explains and grounds its answer in what you are studying; asking is recorded as a learning signal, not a grade.
Images, PDF or text. Kept on this device only.
Keeping your files on this device
Off by default. The mentor always gets your file; this only decides whether your own copy stays here. Copies live in this browser only - they do not follow you to another device, and clearing site data removes them.
Ctrl/Cmd + Enter to send
Rung 1 — the average of a d8
Loading exercise…
Rung 2 — the expected damage of an attack
Loading exercise…
Loading the exercise...
My notes on this module
Loading your notes...
Expected value: the average outcome of a choice — TransformerLab