Conditional Expected Second Roll
Problem statement
You have two dice with sidesA and sidesB sides. For this exercise, assume each die is fair and its faces are the consecutive integers from 1 through its side count. The two dice remain distinct even when their side counts are equal.
For this exercise, assume one die is selected uniformly at the start, so each die has probability 1/2. Keep that same selected die and roll it twice. The rolls are independent conditional on the chosen die.
The first roll is observed to be observedFirstRoll. Return the expected value of the second roll conditional on that observation. The observed value is guaranteed to be possible for at least one die. Do not select a new die for the second roll.
For this exercise, assume the side counts and first observed face are inputs generalizing the reported 6-sided/4-sided question with first roll 2. Complete expectedSecondRoll(sidesA, sidesB, observedFirstRoll) and return a double, without rounding to an integer.
An answer is accepted when abs(actual - expected) <= 1e-9 * max(1, abs(expected)), using a relative-or-absolute tolerance of 1e-9.
Function
expectedSecondRoll(sidesA: int, sidesB: int, observedFirstRoll: int) → doubleExamples
Example 1
sidesA = 6sidesB = 4observedFirstRoll = 2return = 2.9This is the reported fixed numerical instance. Under the stated fair-dice and equal-selection assumptions, the posterior weights are 2/5 and 3/5. Their second-roll means are 3.5 and 2.5, so the conditional expectation is 2.9.
Example 2
sidesA = 6sidesB = 4observedFirstRoll = 6return = 3.5Only the six-sided die can show the first face 6. Its second roll has mean 3.5. The original-author clarification uses this observation to explain why the posterior must change.
Example 3
sidesA = 4sidesB = 6observedFirstRoll = 5return = 3.5This authored practice example swaps the die order and observes a face available only on the six-sided die. Return that die's mean 3.5.
Example 4
sidesA = 5sidesB = 5observedFirstRoll = 3return = 3.0This authored practice example gives both distinct dice the same side count. Both means are 3, so the posterior mixture also has mean 3.
Constraints
- For this exercise, assume
2 <= sidesA, sidesB <= 10^6. 1 <= observedFirstRoll <= max(sidesA, sidesB).- Both rolls use the die selected once before the first roll.
- The returned expectation uses the posterior probabilities after observing the first roll.