Menu
CoddyTech

Sums of Dice

Part of the Statistics for ML section of Coddy's Machine Learning journey. Lesson 15 of 64.

Roll two dice and add them: 7 comes up six times as often as 2. Count the pairs of faces:

A 6 by 6 grid of the totals of two dice, first die down the side and second die across the top. The six cells that make 7 run corner to corner and are shaded; only one cell makes 2

A simulation agrees. Add the dice roll by roll, then count each total; value_counts lists the most common first:

rng = np.random.default_rng(2)
first = rng.integers(1, 7, size=3600)
second = rng.integers(1, 7, size=3600)
counts = pd.Series(first + second).value_counts()
print(counts.head(3))

This prints:

7    605
6    522
8    443
Name: count, dtype: int64

ax.bar(counts.index, counts) draws the whole tent:

A bar chart of 3600 rolls of two dice: the bars rise from about 100 at a total of 2 to a peak of about 600 at 7, then fall back to about 100 at 12

A bar chart like this is your first look at any label: its rare values are the ones a model will barely learn.

challenge icon

Challenge

Medium

In a role-playing game, damage is the total of two four-sided dice, 1 to 4 each. The program reads how many attacks; the starter rolls into first and second and labels the chart.

  1. Count each damage total.
  2. Print the most likely damage and its share of the attacks, to 3 decimals.
  3. Draw the counts as bars, one per total, and show them.

For 16 attacks, too few to trust:

Most likely damage: 6, share 0.312

A bar chart titled Damage Rolls for 16 attacks: bars at 3, 5, 6, 7 and 8, the tallest at 6 with 5 rolls

Then Coddy describes your chart. With more attacks, the peak settles.

Try it yourself

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

rolls = int(input())

# Don't change the lines below
rng = np.random.default_rng(4)
first = rng.integers(1, 5, size=rolls)
second = rng.integers(1, 5, size=rolls)

fig, ax = plt.subplots()
ax.set_title("Damage Rolls")
ax.set_xlabel("Damage")
ax.set_ylabel("Rolls")

# Write your code below
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Statistics for ML

Practice on your own: Online Python compiler