Reroll Game Expected Value and Break-Even Cost
Math & Bit ManipulationAuto-graded · 21 test casesLast updated
Tests probability recursion, expected value with random stopping times, and cost-benefit analysis in a sequential game.
You are analyzing a sequential dice game with the following rules:
- A biased die is rolled repeatedly
- After each roll, you collect points equal to the face value
- With probability
p, the roll forces you to continue (roll again) - With probability
1 - p, the roll ends the game - The expected value of a single roll is
mu
Your task is to compute two quantities:
- Expected total payout: What is the expected sum of all face values collected before the game ends?
- Break-even cost: If each roll after the first costs
cpoints, what value ofcmakes the game fair (expected net payout = 0)?
Return a list [expected_payout, break_even_cost].
Special cases:
- If
p >= 1, the game never terminates; return[None, None] - If
p == 0, only one roll occurs; return[mu, None](no rerolls mean no reroll cost applies)
Constraints
0 <= p <= 1mu > 0(expected roll value is positive)- Return floating-point values with standard precision
- The break-even cost is the value such that
expected_payout - c * expected_rerolls = 0
Examples
Example 1
Input: reroll_game_analysis(0.5, 3.5)
Output: [7, 7]
Example 2
Input: reroll_game_analysis(0.25, 4)
Output: [5.333333333333333, 16]
Example 3
Input: reroll_game_analysis(0.8, 3.2)
Output: [16.000000000000004, 4]
Hints
Hint 1
Set up a recursion: if E is the total expected payout, then E = mu + p·E, since with probability p you restart the entire process after collecting mu.
Hint 2
The expected number of rolls follows a geometric distribution with success probability (1-p). Use this to find how many paid rerolls occur.
Hint 3
For break-even: compute the expected number of rerolls (not total rolls), multiply by cost c, and set net payout to zero.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This problem requires setting up a recursive expectation and analyzing a geometric stopping process.
Part 1: Expected Total Payout
Let E denote the expected total payout. We use first-step analysis: after the first roll, we collect mu points on average. With probability p, we must continue rolling, and the future payout has the same distribution as the original problem (memoryless property).
This gives us the recursion:
E = mu + p · E
Solving for E:
E - p·E = mu
E(1 - p) = mu
E = mu / (1 - p)
Why this works: Each roll contributes mu to the expectation. The game continues with probability p, creating a geometric series. The expected number of rolls is 1/(1-p), so by Wald's identity, the total expected payout is mu · 1/(1-p).
Part 2: Break-Even Reroll Cost
The first roll is free; subsequent rolls each cost c. The expected number of total rolls is 1/(1-p), so the expected number of paid rerolls is:
E[rerolls] = 1/(1-p) - 1 = p/(1-p)
The net expected payout is:
E[net] = mu/(1-p) - c · p/(1-p) = (mu - c·p)/(1-p)
Setting E[net] = 0 to find break-even:
mu - c·p = 0
c = mu/p
Edge case: When p = 0, the game ends after one roll with no rerolls possible, so reroll cost is undefined (return None).
Edge case: When p >= 1, the game never terminates, so both values are undefined.
Complexity
- Time: O(1) – closed-form arithmetic
- Space: O(1) – constant storage
Solution
from typing import List, Optional
def reroll_game_analysis(p: float, mu: float) -> List[Optional[float]]:
if p >= 1:
return [None, None]
expected_payout = mu / (1 - p)
if p == 0:
break_even_cost = None
else:
break_even_cost = mu / p
return [expected_payout, break_even_cost]