Unit 3 · Programming 2 · Lesson 3.18 · Lab

Lab: Random Day

Put randomness to work. Roll a die a thousand times and count the sixes — is it fair? — then build a magic 8-ball or rock-paper-scissors. Randomness meets the accumulator, and it’s the on-ramp to Unit 4 simulations.

Big Idea 3 — Algorithms & ProgrammingLab · 10 points

Before this lab

No reading — build. Keep 3.17 (random), 3.4 (accumulator), and 2.13 (elif) open. Pick one program below to build fully.

1

Where We Are

People→ Bits→ Programs→ Internet→ Security→ Big Data→ Impact

Still in Programs, late Week 4. Randomness in a loop with a counter is your first simulation — run a chance experiment many times and measure what happens. That’s literally what Unit 4 opens with.

2

The Brief

Build one of these (a second if you have time):

Pick your program

□ Dice experiment — roll a die 1000 times, count how many are sixes, and print the count and the fraction. Then ask: is it close to 1⁄6? (accumulator + random)

□ Magic 8-ball — pick a random answer from a list of replies and print it. (random + lists)

□ Rock-paper-scissors — the computer picks randomly, you compare with an elif chain and report win/lose/tie. (upgrade of the 2.15 idea)

Requirements (whichever you pick)

□ A purpose sentence comment at the top.

□ At least one call to random (randint).

□ A loop with an accumulator/counter (dice), OR a list (8-ball), OR a conditional (RPS).

3

The Model — the dice experiment

This is a simulation in seven lines: repeat a random trial many times and count the outcome you care about.

Python

# Counts sixes in 1000 rolls to
# check whether the die is fair.
import random
sixes = 0
for i in range(1000):
    roll = random.randint(1, 6)
    if roll == 6:
        sixes = sixes + 1
print(sixes, sixes / 1000)

What each part is doing

# purpose: what + why
#
# bring in random
# accumulator: initialize
# run 1000 trials (count loop)
# one random roll per trial
# selection: is it a six?
# count it (update)
# use: expect about 1/6 ≈ 0.167

Hover or tap a line to light its twin. Every ingredient of Unit 3 is here: a library, a loop, a conditional, and an accumulator. A fair die should land near 1000 ÷ 6 ≈ 167 sixes.

4

Try It — Run the Experiment

Roll the die many times and watch the count of sixes. Run it a few times: the exact number jumps around, but the fraction settles near 1⁄6. That’s a simulation in action.

rolls =
press Run to roll the die
5

What You Turn In

Three things

□ The working program (uses random, runs correctly).

□ Your purpose sentence at the top.

□ One sentence on what you observed — e.g. “across runs the six-count changed but the fraction stayed near 1⁄6” — and whether that matches a fair die.

That observation sentence is the start of Unit 4: using a simulation to answer a question (“is it fair?”) instead of just producing output.

6

How to Work

Get it working on 10 rolls first

Run your dice loop for just 10 rolls at first and print each roll, so you can see it working and confirm the counter increments only on sixes. Then bump it to 1000 and remove the per-roll printing. Debugging a loop is far easier at 10 than at 1000.

Solo or paired — if you pair, swap every 10 minutes; both of you should be able to point to the accumulator and the random call.

7

Challenge Tier

If you finish early

Count every face, not just sixes: use a list of six counters and tally each roll, then check they’re all near 167 — a preview of Unit 5’s frequency tables. Or, for rock-paper-scissors, add a running score across rounds and play best-of-5. Or turn the dice experiment into a procedure count_sixes(n) that takes the number of rolls as a parameter and returns the count — the Create PT shape, again.

8

Connections

Uses the whole unit — library (3.16), random (3.17), a loop, a conditional, and an accumulator (3.4).
Next in 3.19 — Pseudocode Gym II: reading exam-style programs that combine all of this, plus your first Robot questions.
Opens Unit 4 — “run a random trial many times and measure” is the definition of a simulation.
Returns on the exam — simulation and “expected result of many random trials” questions build directly on this.
← 3.17 Random Values