Code

Roll Many Dice

Is a die really fair? Roll it 120 times and count! Scientists call this a simulation: let the computer try something many times.

import random

rng = random.Random(7)     # a dice roller with seed 7
print(rng.randint(1, 6))   # 1 to 6, both included

random.Random(seed) makes your own random number generator. The seed is its starting point: the same seed always gives the same rolls. That makes experiments repeatable, and testable.

Count results in a dict that starts at zero for every face:

counts = {face: 0 for face in range(1, 7)}
counts[3] += 1

A bar chart

Draw each count as a column of pixels growing up from the bottom. A bar of height h covers rows canvas.height - h to canvas.height - 1.

Assignment

  1. roll_counts(rolls, seed): make rng = random.Random(seed), call rng.randint(1, 6) exactly rolls times, and return a dict with keys 1 to 6 (zero if a face never came up).
  2. draw_bars(canvas, counts, scale): for each face f, paint column x = (f - 1) * 2 "gold" from the bottom up. Its height is counts[f] // scale, but never more than canvas.height.