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.
Focus on the range a random call can produce. Enrolled in our Runestone course? Open it from there so your progress counts.
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.
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.
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.
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.
import random roll = random.randint(1, 6) # 1..6 flip = random.randint(0, 1) # 0 or 1
/* 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.
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.
Set a range and roll. You can’t predict any single roll — but watch how every result stays inside the bounds, both ends included.
randint(a,b) /
RANDOM(a,b) include both ends.Five questions. Remember: both ends are included, and you predict the range, not the value. Then pick your answer.
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.
random is a library you import; randint
is one of its procedures.RANDOM(2, 5)?” is a guaranteed item. Include both ends.