Unit 3 · Programming 2 · Lesson 3.17

Random Values

Randomness is how programs roll dice, shuffle cards, and make games different every time. You can’t predict the exact value — but you can always predict the range, and that’s what the exam asks.

Big Idea 3 — Algorithms & ProgrammingA place the two languages agree

Before this lesson — read on Runestone

Focus on the range a random call can produce. Enrolled in our Runestone course? Open it from there so your progress counts.

1

Where We Are

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

Still in Programs. You imported random in 3.16; today you use it. Randomness plus a loop and an accumulator gives you a simulation — the bridge straight into Unit 4.

2

The Hook

Why is every game of solitaire different? Because the deck is shuffled with random numbers. Dice, loot drops, matchmaking, which ad you see, the “shuffle” on your playlist — all powered by a call that returns an unpredictable value in a known range.

random.randint(1, 6) gives you a die roll: you can’t know if it’s a 3 or a 5, but you know for certain it’s between 1 and 6, inclusive. Predicting the range, not the value, is the whole exam skill.

3

The Idea

random.randint(a, b) returns a random whole number from a to b, including both ends. AP Pseudocode’s RANDOM(a, b) works exactly the same way — a rare spot where the two languages agree completely.

Both ends are included. Unlike range() (which stops before its end), randint(1, 6) and RANDOM(1, 6) can both produce a 1 or a 6. The count of possible outcomes is b − a + 1 — for 1 to 6, that’s 6 outcomes.

Python

import random
roll = random.randint(1, 6)  # 1..6
flip = random.randint(0, 1)  # 0 or 1

AP Pseudocode

/* RANDOM is built in */
roll ← RANDOM(1, 6)
flip ← RANDOM(0, 1)

Hover or tap a line to light its twin. A coin is RANDOM(0, 1) — two outcomes. Note how cleanly the notations match here; enjoy it, because the index and loop flips do not.

Predict the range, never the value. A trace question can’t ask “what does randint(1, 6) return” — nobody knows. It asks “which of these values is possible?” So the skill is bounding the output: lowest possible, highest possible, and how many outcomes. Off-by-one on the ends is the classic trap.
4

Try It — The Range Roller

Set a range and roll. You can’t predict any single roll — but watch how every result stays inside the bounds, both ends included.

RANDOM( , )
possible values: —
this roll: —
recent rolls: —
5

Vocabulary

random Programs
A value the program cannot predict, drawn from a known range. randint(a,b) / RANDOM(a,b) include both ends.
simulation Programs
Using random values in a loop to imitate a real, chance-driven process (dice, weather, traffic) — the big idea of Unit 4.
6

Check

Five questions. Remember: both ends are included, and you predict the range, not the value. Then pick your answer.

0 of 5 answered
7

Impact Check

Fair dice, loaded dice, and things that only look random

Randomness runs games, lotteries, scientific simulations, and the security keys that protect your messages (Unit 7). But “random” is a design choice with consequences: a shuffle that isn’t truly even, a loot system tuned to feel generous but pay out rarely, a “random” playlist that quietly favors certain songs. The range you allow, and how evenly you draw from it, shape real outcomes.

There’s also a deep catch: computers can’t make true randomness on their own — they compute pseudo-random numbers that look random but follow a formula. Usually fine; occasionally a security disaster when someone predicts the “random” keys. Real unpredictability is harder than it looks.

8

Connections

Came from 3.16 — random is a library you import; randint is one of its procedures.
Next in 3.18 — Random Day: roll 1000 times and count the sixes — randomness meets the accumulator.
Opens Unit 4 — randomness in a loop is a simulation, the heart of the next unit.
Returns on the exam — “which value is a possible result of RANDOM(2, 5)?” is a guaranteed item. Include both ends.
← 3.16 Libraries and APIs