AP CSP Topic 3.15 Guided Notes - Random Values

Big Idea 3: Algorithms and Programming · Topic 3.15 · Guided Notes (Student)

Random Values — Guided Notes

Fill these in during class or catch up here if you were absent. Print this page or work on paper — then check yourself with the CFUs on the Topic 3.15 page.

Print these notesAll CSP topics

Today’s objectives

  • Write RANDOM(a, b) expressions to generate a random integer across a chosen range, knowing both bounds are inclusive and every value is equally likely (LO AAP-3.E)
  • Evaluate a RANDOM expression to list its complete set of possible results and explain why each execution may produce a different value (LO AAP-3.E)

Bell ringer

A mystery-reward machine picks its prize by generating a number from 1 to 3: 1 = coins, 2 = a gem, 3 = a key. With a partner, 'run' the machine six times using any fair method (roll a die and re-roll on 4–6, or each secretly pick 1, 2, or 3 and reveal). Record the six prizes in order.

Write 2–4 sentences: Before you ran it, could you have predicted the EXACT six-prize sequence? Could you have predicted the complete list of prizes that were even POSSIBLE? What is the difference between those two questions?

01. Writing RANDOM Expressions

Key Vocabulary (LO AAP-3.E)

Term Definition (write it)
Random value
RANDOM(a, b)
Inclusive
Equally likely
Possible results

RANDOM(a, b): a Random Integer, Inclusive

  • In RANDOM(a, b), the values a and b are themselves .
  • RANDOM treats every value in its range as .
  • The programmer controls the range, but .

Generate a Random Integer

RANDOM(1, 6) can return 1, 2, 3, 4, 5, or 6. The value changes every run — you cannot predict which.

AP Pseudocode (what the exam tests)

roll ← RANDOM(1, 6)
DISPLAY(roll)

Python (the runnable version)

import random
roll = random.randint(1, 6)
print(roll)

Run and edit this yourself in the coding exercises for this topic.

What You Set vs. What You Get

You control the bounds

You can't control the outcome

  • A programmer can always determine a RANDOM call's , but not which one occurs next.
  • Running the exact same RANDOM expression twice may .

AP TIP: On the exam, never answer 'RANDOM returns 4.' Answer with the SET — 'any of 1, 2, 3, 4, 5, or 6, each equally likely.'

Stop and think

  1. Write a single RANDOM expression that could return any whole number from 1 to 6, and list all six values it could produce.
  2. You need a random value that is either 0 or 1 (a coin flip stored as 0 or 1). Write the RANDOM call and explain why RANDOM(0, 1) — not RANDOM(0, 2) — is correct.
  3. A game needs a random floor number from 10 to 20 inclusive. Write the expression and give the number of different floors it could choose.

Answer in complete sentences. Then check yourself with the matching CFUs on the Topic 3.15 page.

02. The Set of Possible Results

Every Value from a to b, and Each Run May Differ

  • To list the possible results of RANDOM(a, b), you take .
  • The count of possible outcomes is b − a + 1, where the extra one exists because .
  • You can state the complete set of outcomes with certainty, yet .

You Can't Print the Number — Print a Property

The roll changes every run, but it is ALWAYS between 1 and 6, so "in range" prints every single time.

AP Pseudocode (what the exam tests)

roll ← RANDOM(1, 6)
IF(roll ≥ 1 AND roll ≤ 6)
{
  DISPLAY("in range")
}

Python (the runnable version)

import random
roll = random.randint(1, 6)
if 1 <= roll <= 6:
    print("in range")

Output

in range

Run and edit this yourself in the coding exercises for this topic.

List the Possible Results

For each RANDOM call, list every value it could return and count them. Remember: both bounds are included, so the count is b − a + 1.

Complete the empty cells.

RANDOM call Possible results How many
RANDOM(1, 3) 1, 2, 3 3
RANDOM(5, 8) 4
RANDOM(0, 1) 0, 1 2
RANDOM(4, 4) 4
RANDOM(2, 6) 5

Watch out — “RANDOM(1, 6) Tops Out at 5”

Myth: RANDOM(1, 6) returns values from 1 up to 5 — the second number, 6, is an upper limit that is never actually reached.

Explain why this is wrong:

Stop and think

  1. List every possible result of RANDOM(3, 7) and state how many there are. Show the b − a + 1 count.
  2. A program runs DISPLAY(RANDOM(1, 4)) and prints 2. A classmate says, 'so the program always prints 2.' Correct them using the idea that each execution may differ.
  3. Two students run the exact same RANDOM(1, 100) line and get different numbers. Explain why this is expected behavior, not a bug.
  4. In one sentence, give the rule for the number of possible outcomes of RANDOM(a, b), and say why the '+ 1' is there.

Answer in complete sentences. Then check yourself with the matching CFUs on the Topic 3.15 page.

Common AP Traps

Three ways Topic 3.15 loses points on the exam — one minute now, real points in May.

Both bounds are included — in your own words:

Name the set, not the next value — in your own words:

The bounds set the whole range — in your own words:

Random Values, in One Slide

  • RANDOM(a, b) returns a random integer from a to b, INCLUSIVE, and every value is equally likely.
  • The possible results run from a to b, so there are b − a + 1 outcomes; RANDOM(1, 3) can return 1, 2, or 3.
  • Each execution may produce a different result — you can predict the set of possibilities, never the next value.
  • Because the output varies, tested random programs check a PROPERTY (is the result in range?) instead of the raw number, so the expected result stays predictable.

Exit check — I can…

  • ☐  write a RANDOM(a, b) expression to generate a random integer across a chosen inclusive range (LO AAP-3.E)
  • ☐  list the complete set of possible results of a RANDOM expression and count them as b − a + 1 (LO AAP-3.E)
  • ☐  explain why each execution of a random program may produce a different result (LO AAP-3.E)
  • ☐  explain why a tested random program checks a property of the result rather than the exact value (LO AAP-3.E)

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.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]