Calculate Expected Return from Two Six-Sided Dice
Math & Bit ManipulationAuto-graded · 17 test casesLast updated
Computes the expected payoff of a game played with two independent six-sided dice, rounded to two decimal places.
Consider a probability game played with two fair, standard six-sided dice (with faces numbered 1 through 6).
The payoff rules for a single roll of both dice are defined as follows:
- If both dice display different values, your payout is the greater of the two face values.
- If both dice land on the same value (a matching pair), you lose an amount equal to the total sum of the two dice (i.e., a payout of $-(a + b)$ for outcome $(a, b)$).
Write a function calculate_expected_payoff that determines the expected value (mean outcome) of the payoff for a single round of this game. Return the expected value rounded to two decimal places.
Constraints
- The dice are standard six-sided dice with face values $1, 2, 3, 4, 5, 6$.
- Both dice are independent and fair, yielding 36 equally likely outcomes.
Examples
Example 1
Input: calculate_expected_payoff()
Output: 2.72
Example 2
Input: calculate_expected_payoff()
Output: 2.72
Hints
Hint 1
Identify the total number of equally likely outcomes when rolling two 6-sided dice.
Hint 2
Divide the outcomes into two cases: matching outcomes (6 pairs) and non-matching outcomes (30 pairs).
Hint 3
Sum the payout for every outcome, then divide by 36 and round to 2 decimal places.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
There are $6 \times 6 = 36$ distinct and equiprobable ordered outcomes when rolling two fair 6-sided dice. Because all outcomes occur with probability $1/36$, the overall expected value is simply the sum of the payoffs across all 36 outcomes divided by 36.
We can iterate over every ordered pair $(d_1, d_2)$ where $1 \le d_1, d_2 \le 6$:
-
Doubles ($d_1 = d_2$): There are 6 such outcomes. For a face value $d$, the payoff is $-(d + d) = -2d$. The total penalty across all doubles is: $$\sum_{d=1}^6 (-2d) = -2 \times (1 + 2 + 3 + 4 + 5 + 6) = -2 \times 21 = -42$$
-
Non-doubles ($d_1 \neq d_2$): There are $36 - 6 = 30$ such outcomes. For a fixed maximum value $m \in {2, 3, 4, 5, 6}$, there are $2(m - 1)$ pairs where $\max(d_1, d_2) = m$.
- $m = 2$: $2 \times 1 = 2$ outcomes, contributing $2 \times 2 = 4$
- $m = 3$: $2 \times 2 = 4$ outcomes, contributing $3 \times 4 = 12$
- $m = 4$: $2 \times 3 = 6$ outcomes, contributing $4 \times 6 = 24$
- $m = 5$: $2 \times 4 = 8$ outcomes, contributing $5 \times 8 = 40$
- $m = 6$: $2 \times 5 = 10$ outcomes, contributing $6 \times 10 = 60$
Summing these contributions yields $4 + 12 + 24 + 40 + 60 = 140$.
Combining both parts, the total payout sum across all 36 outcomes is $140 - 42 = 98$.
The expected value is: $$E = \frac{98}{36} = \frac{49}{18} \approx 2.7222\dots$$
Rounding to two decimal places gives 2.72.
Complexity Analysis
- Time Complexity: $\mathcal{O}(1)$ since the sample space is fixed at 36 pairs.
- Space Complexity: $\mathcal{O}(1)$ auxiliary memory required.
def calculate_expected_payoff() -> float:
total_payoff = 0
for die1 in range(1, 7):
for die2 in range(1, 7):
if die1 == die2:
total_payoff -= (die1 + die2)
else:
total_payoff += max(die1, die2)
return round(total_payoff / 36, 2)