Session 12 Two Dice Roll and Plot
In this session, we discussed student generated programs to simulate rolling two dice and counting the sums of the two dice. The beginner friendly approach taken by the students involved starting twelve counters for each possible sum (2-12) and incrementing the appropriate counter based on the sum of the two dice.
We also thought about how to visualise the results of the simulation. We discussed the following program that uses lists and dictionaries to store the sums and their frequencies. The program also uses matplotlib to plot the results.
import random
import matplotlib.pyplot as plt
NUM_TRIALS = 216
# Simulate 216 tosses of two dice and record the sum for each toss.
sums = []
for _ in range(NUM_TRIALS):
die1 = random.randint(1, 6)
die2 = random.randint(1, 6)
sums.append(die1 + die2)
# Count the frequency of each possible sum from 2 to 12.
sum_counts = {total: 0 for total in range(2, 13)}
for total in sums:
sum_counts[total] += 1
# Prepare data for plotting.
values = list(sum_counts.keys())
frequencies = [sum_counts[total] for total in values]
plt.figure(figsize=(8, 5))
plt.plot(values, frequencies, marker="o", color="skyblue", linewidth=2)
plt.title(f"Frequency of Two-Dice Sums over {NUM_TRIALS} Tosses")
plt.xlabel("Sum of two dice")
plt.ylabel("Frequency")
plt.xticks(values)
plt.grid(axis="y", linestyle="--", alpha=0.5)
plt.tight_layout()
# Optionally show the plot and save it.
plt.show()