Dice Duel Probability with Optional Blind Reroll
Math & Bit ManipulationAuto-graded · 21 test casesLast updated
Calculate the exact win probability in a dice duel where one player uses an optimal single blind reroll strategy.
Two players, Alice and Bob, play a dice game. Alice rolls an $m$-sided die with integer face values uniformly distributed from $1$ to $m$. Bob rolls an $n$-sided die with integer face values uniformly distributed from $1$ to $n$. All rolls are mutually independent.
Bob wants to maximize his probability of winning. He sees the result of his first roll, but does not see Alice's outcome. Bob may choose to reroll his die at most once. If he rerolls, he must keep the second roll's result and discard the first roll. Alice has no rerolls.
After Bob establishes his final number and Alice rolls her die:
- Alice wins if her final roll is strictly greater than Bob's final roll.
- Bob wins if his final roll is greater than or equal to Alice's final roll (ties are awarded to Bob).
Assuming Bob plays optimally to maximize his chance of winning, return the probability that Alice wins.
Constraints
- $1 \le m \le 1000$ (represented as
alice_sides) - $1 \le n \le 1000$ (represented as
bob_sides)
Examples
Example 1
Input: alice_win_probability(30, 20)
Output: 0.5666666666666668
Example 2
Input: alice_win_probability(1, 1)
Output: 0
Example 3
Input: alice_win_probability(6, 6)
Output: 0.2916666666666667
Hints
Hint 1
Determine Bob's win probability for a fixed final roll $b$.
Hint 2
Since Bob does not see Alice's roll before deciding to reroll, the expected win probability of a second roll is fixed and independent of his first roll.
Hint 3
Bob should keep his first roll $b$ if and only if his win probability with $b$ is greater than or equal to his expected win probability from rerolling.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
To solve this problem, we evaluate Bob's decision-making process:
-
Bob's Win Condition for a Given Outcome $b$: For any final roll $b$ by Bob, Bob wins whenever Alice's roll $A \le b$. Since Alice's die is uniform over ${1, \dots, a}$, Bob's probability of winning with value $b$ is: $$\text{keep_bob}(b) = \frac{\min(b, a)}{a}$$
-
Bob's Expected Win Probability upon Rerolling: If Bob chooses to reroll, his outcome is uniform on ${1, \dots, b}$. His expected probability of winning after a reroll is the average of $\text{keep_bob}(x)$ for $x \in {1, \dots, b}$: $$\text{reroll_bob} = \frac{1}{b} \sum_{x=1}^{b} \frac{\min(x, a)}{a}$$
-
Optimal Strategy: Bob decides whether to keep his initial roll $f \in {1, \dots, b}$ by comparing his immediate win probability $\text{keep_bob}(f)$ with $\text{reroll_bob}$.
- If $\text{keep_bob}(f) \ge \text{reroll_bob}$, Bob keeps $f$. Alice's probability of winning in this case is $\frac{\max(a - f, 0)}{a}$.
- If $\text{keep_bob}(f) < \text{reroll_bob}$, Bob rerolls. Alice's probability of winning is the average probability that Alice beats a fresh reroll: $$\text{reroll_alice} = \frac{1}{a \cdot b} \sum_{x=1}^{\min(b, a-1)} (a - x)$$
-
Overall Win Probability for Alice: By iterating through all possible initial rolls $f \in {1, \dots, b}$, we compute Alice's win probability under Bob's optimal policy and average over the $b$ possible initial rolls.
Complexity
- Time Complexity: $\mathcal{O}(m + n)$, as calculating closed-form summations takes $\mathcal{O}(1)$ time and iterating over Bob's initial rolls takes $\mathcal{O}(n)$ steps.
- Space Complexity: $\mathcal{O}(1)$ additional space.
def alice_win_probability(alice_sides: int, bob_sides: int) -> float:
a = alice_sides
b = bob_sides
# Bob's probability of winning if he rerolls, averaged over the
# second roll. Ties count as Bob wins.
m = min(a, b)
reroll_bob = (m * (m + 1) / 2 + (b - m) * a) / (a * b)
# Alice's probability of winning after a reroll.
k = min(b, a - 1)
reroll_alice = (k * a - k * (k + 1) / 2) / (a * b)
alice_total = 0.0
for first in range(1, b + 1):
keep_bob = min(first, a) / a
if keep_bob >= reroll_bob:
alice_total += max(a - first, 0) / a
else:
alice_total += reroll_alice
return alice_total / b