Adding Up Two Homemade Dice

Random VariablesEasy+10 XP+10

Adding Up Two Homemade Dice

A board-game designer is testing homemade dice. Each die is written as the list of numbers printed on its faces, one entry per face. Every face is equally likely to land on top, but the same number may be printed on more than one face. For example, [1, 1, 2] is a three-faced die that shows 1 with probability 2/32/32/3 and 2 with probability 1/31/31/3. The two dice do not need the same number of faces.

The game rolls both dice and uses their total. Let TTT be that total: a discrete random variable.

Task: write sum_pmf(die_a, die_b) that returns the PMF of TTT, P(T=t)P(T = t)P(T=t), as a list of (total, probability) tuples:

  • one tuple for every total that can actually occur (probability above 0), sorted from the smallest total to the largest;
  • probabilities rounded to 4 decimal places.

The two dice are rolled independently. Faces can be any whole numbers, including 0 and negatives, and each die has at least one face.

For instance, two dice that each show [0, 1] give [(0, 0.25), (1, 0.5), (2, 0.25)].

Examples

die_a = [1, 2, 3, 4, 5, 6]
die_b = [1, 2, 3, 4, 5, 6]
returns [(2, 0.0278), (3, 0.0556), (4, 0.0833), (5, 0.1111), (6, 0.1389), (7, 0.1667), (8, 0.1389), (9, 0.1111), (10, 0.0833), (11, 0.0556), (12, 0.0278)]
die_a = [0, 0, 5]
die_b = [0, 5, 5, 5]
returns [(0, 0.1667), (5, 0.5833), (10, 0.25)]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run