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
roll_counts(rolls, seed): makerng = random.Random(seed), callrng.randint(1, 6)exactlyrollstimes, and return a dict with keys1to6(zero if a face never came up).draw_bars(canvas, counts, scale): for each facef, paint columnx = (f - 1) * 2"gold"from the bottom up. Its height iscounts[f] // scale, but never more thancanvas.height.
roll_counts(rolls, seed): makerng = random.Random(seed), callrng.randint(1, 6)exactlyrollstimes, and return a dict with keys1to6(zero if a face never came up).draw_bars(canvas, counts, scale): for each facef, paint columnx = (f - 1) * 2"gold"from the bottom up. Its height iscounts[f] // scale, but never more thancanvas.height.
main.py Python sleeping
Press Run to try your code, then Check to test it.