Big Idea 3: Algorithms and Programming · Topic 3.15 · Coding Practice
Random Values — Code It Yourself
Write real code and check it against the expected output. Pick Python or JavaScript; the AP pseudocode reference is there when you need it.
You warmed these up on paper in Read the Random Rolls and Design the Random Range, listing the possible results of GameNight's RANDOM calls. Now build and run them, hardest last. One rule makes random code testable: because a RANDOM value changes every run, these programs never print the raw random number — they print a PROPERTY that is always true (is it in range? how many outcomes are possible?), so there is one fixed expected output to check. Predict that output first, then run.
Problem 1 of 4
GameNight rolls one six-sided die with RANDOM(1, 6). You cannot print the roll (it changes every run), so instead roll the die and print exactly in range if the roll is between 1 and 6 inclusive; otherwise print out of range.
Expected output: in range
Show AP pseudocode reference
die ← RANDOM(1, 6)
IF(die ≥ 1 AND die ≤ 6)
{
DISPLAY("in range")
}
ELSE
{
DISPLAY("out of range")
}
Every possible value of RANDOM(1, 6) is between 1 and 6, so the condition is always true and the program always prints 'in range'. That is the point: you check a property of the roll, not the roll itself.
Problem 2 of 4
GameNight's color spinner uses RANDOM(0, 3). Without rolling anything, compute and print how many DIFFERENT values that call could return. Use the count b - a + 1 so it works for any bounds.
Expected output: 4
Show AP pseudocode reference
a ← 0
b ← 3
count ← b - a + 1
DISPLAY(count)
The number of possible results of RANDOM(a, b) is b - a + 1 because both bounds are included. For RANDOM(0, 3): 3 - 0 + 1 = 4 (the values 0, 1, 2, 3).
Problem 3 of 4
GameNight rolls TWO six-sided dice, RANDOM(1, 6) each, and adds them. The possible totals run from 2 to 12. Roll both, add them, and print exactly valid sum if the total is between 2 and 12 inclusive; otherwise print bad sum.
Expected output: valid sum
Show AP pseudocode reference
d1 ← RANDOM(1, 6)
d2 ← RANDOM(1, 6)
total ← d1 + d2
IF(total ≥ 2 AND total ≤ 12)
{
DISPLAY("valid sum")
}
ELSE
{
DISPLAY("bad sum")
}
Each die is at least 1 and at most 6, so the sum is at least 2 and at most 12 — always. The total varies every run, but the property 'total is between 2 and 12' is guaranteed, so 'valid sum' always prints.
Problem 4 of 4
Write a fairness check from scratch for GameNight's 4-sided die, RANDOM(1, 4). Roll it 1000 times in a loop. If EVERY roll is between 1 and 4 inclusive, print exactly all fair; otherwise print unfair.
Expected output: all fair
Show AP pseudocode reference
result ← "all fair"
REPEAT 1000 TIMES
{
roll ← RANDOM(1, 4)
IF(roll < 1 OR roll > 4)
{
result ← "unfair"
}
}
DISPLAY(result)
Assume 'all fair' before the loop, and only switch to 'unfair' if a roll ever lands outside 1..4. Because RANDOM(1, 4) can only return 1, 2, 3, or 4, no roll is ever out of range — so across all 1000 independent, differing rolls the result stays 'all fair' every run. That guaranteed property is what the grader checks.
Your code runs on a secure external service. Answers are checked automatically — nothing is stored.
Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed.
Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Typically responds within 24 hours
✓
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.