The method
A game of chance against nature is solved by giving every state a value: the expected payoff from that state under optimal play. Then
The work is choosing the state so that it captures everything that matters for the future, and finding an order in which to fill in values so that each one uses only values already known:
- Finite horizon (at most rolls): the state includes the number of rolls left, and you fill in from "no rolls left" upwards.
- Shrinking resources (cards leave a deck): the state is what is left, and you fill in from the empty deck upwards.
- Stationary problems (unlimited rerolls): the state looks the same after every reroll, so the value solves a fixed-point equation instead of a recursion.
This lesson is single-player: the only opponent is chance. Games where another player chooses (bargaining, auctions, sequential games) are solved by the same backward logic but need equilibrium ideas, covered in the game theory module.
Limited rerolls
Roll a fair -sided die (faces ), and after each roll either keep it or reroll, with at most rolls in total. Let be the value with rolls available. With one roll left you keep whatever comes: . With rolls, keep the first roll iff :
Two rules of thumb for interviews:
- The keep-threshold with rolls left is the smallest face above . Write the table one row at a time; never try to reason about the whole game at once.
- Values increase with but with shrinking steps, and the threshold can stay put for a few rows before moving up.
Worked example
a ten-sided die with up to five rolls
A fair die has faces 1 to 10. You are paid the face you keep and may roll up to five times. Build the value table and give the strategy.
Show solutionHide solution
Start from one roll and move up. "Keep" means keep the current roll iff it is at least this face.
- : .
- : keep . .
- : keep . .
- : keep . .
- : still keep , since . .
| Rolls available | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|
| Keep if at least | any | 6 | 7 | 8 | 8 |
| Value | 5.5 | 6.75 | 7.45 | 7.915 | 8.2405 |
Strategy with five rolls: keep an 8, 9 or 10 on the first or second roll, a 7 or better on the third, a 6 or better on the fourth, and take the fifth. A sixth roll would raise the threshold to 9, since .
Unlimited rerolls with a cost
Now each reroll costs and there is no limit. After every roll the future looks the same, so the value (with the first roll free) satisfies
You stop iff the roll beats , the value of paying for another go. Two ways to solve it:
- Guess the threshold. With "stop iff ", the number of rolls is geometric with success probability , so . Maximise over .
- Solve the fixed point. For , put . Then , so , giving and
The square root is the signature of these problems: a small cost per try still costs you a lot, because you search for a long time.
Always confirm a guessed threshold is self-consistent: the threshold you used must be the smallest roll above for the it produces.
Worked example
a twenty-sided die, \$1 per reroll
Roll a fair 20-sided die; the first roll is free and each reroll costs $1. You may reroll as often as you like and are paid the final face. What is the optimal strategy and value?
Show solutionHide solution
With threshold , and .
| 13 | 14 | 15 | 16 | 17 | |
|---|---|---|---|---|---|
| 16.5 | 17 | 17.5 | 18 | 18.5 | |
| Expected paid rerolls | 1.5 | 1.857 | 2.333 | 3 | 4 |
| 15 | 15.143 | 15.167 | 15 | 14.5 |
The best threshold is 15, with .
Consistency check. Rerolling is worth , so stop on 15 or more and reroll on 14 or less: exactly the threshold assumed. Stopping on 14 gives up on average each time you do it, which is why is slightly worse.
A value table: the red and black card game
A deck has red and black cards, shuffled. You turn cards one at a time; each red pays , each black costs . You may stop at any time, keeping your running total. What is the game worth?
The state is , the cards remaining; your past total does not affect future decisions, so it drops out. With (only losses remain, so stop) and (draw everything),
Each entry uses the entry above and the entry to the left, so fill the table in order of :
| 0 | 0 | 0 | 0 | |
| 1 | 1/2 | 0 | 0 | |
| 2 | 4/3 | 2/3 | 1/5 | |
| 3 | 9/4 | 3/2 | 17/20 |
A fair deck () has a positive value even though drawing every card nets exactly zero: the option to stop when ahead is worth something. For the full deck the code below gives about .
from fractions import Fraction
from functools import lru_cache
@lru_cache(maxsize=None)
def value(r, b):
"""Value of the card game with r red and b black cards left."""
if r == 0:
return Fraction(0)
if b == 0:
return Fraction(r)
n = r + b
draw = Fraction(r, n) * (1 + value(r - 1, b)) + Fraction(b, n) * (-1 + value(r, b - 1))
return max(Fraction(0), draw)
print(value(2, 2), value(3, 3))
print(float(value(26, 26)))
Output:
2/3 17/20
2.624475548993925
Contrast this with the red-card game in the optimal stopping lesson, which was worth exactly . There the payoff was a martingale (the chance the next card is red); here the payoff is the running total, and stopping lets you lock in gains that a martingale would have given back.
Worked example
three red, two black
Using the recursion, find and the optimal first moves.
Show solutionHide solution
You need and from the table.
Drawing is worth , so draw. After a red you are at with left, worth : draw again. After a black you are at with left, worth : draw again. Stopping is only right in states with , such as , where the remaining cards are expected to cost you more than the chance of a late red can recover.
Check the edge case : drawing gives . If the red comes first you stop at ; if the black comes first you draw the red and finish at .
Knowledge check
A fair ten-sided die (faces 1 to 10) may be rolled at most twice; you keep the last roll. What is the optimal rule for the first roll and the game's value?
Knowledge check
You draw , are paid the value you stop at, and each redraw costs (the first draw is free). What is the game worth?
Knowledge check
In the red/black card game (red +1, black −1, stop any time), what is the value of a deck with 2 red cards and 1 black card?
Key takeaways
- Value of a state = max(stop payoff, expected value of the next state minus cost). Choose a state that captures everything relevant to the future, and fill values in an order where each uses only known ones.
- Limited rerolls of a -sided die: ; keep iff the roll beats . For a d10: 5.5, 6.75, 7.45, 7.915, 8.24.
- Unlimited rerolls at cost : solve , or maximise over thresholds and check consistency. Uniform case: .
- Card games: tabulate over what is left. The red/black game is worth about 2.62 for a full deck, and the value comes entirely from the option to stop.
Practice problems
Finished reading?
Answer the 3 knowledge checks to complete this lesson (0 done).