Topic 3.15: Random Values | AP CSP Big Idea 3 | APCSExamPrep.com

AP CSP Course Big Idea 3 3.15 Random Values
3.15
Big Idea 3 • Algorithms & Programming

Random Values

🕐 ~40 min FREE 📖 6 MCQ questions 🎮 Random Ranger game 💻 Live Python editor AAP-3.E

After this lesson, you will be able to:

  • Explain why programs use random values for games, simulations, and sampling
  • Use RANDOM(a, b) and random.randint(a, b) to generate a random integer
  • Know that both endpoints a and b are possible results
  • Count the possible outcomes of a range with the b - a + 1 rule
  • Seed the generator to make random output repeatable for testing
  • Use a random value inside a conditional or loop for your Create Performance Task
📈 Exam weight: Random values appear in tracing and reasoning items across Big Idea 3, and they are a favorite way to test the inclusive range and the b - a + 1 count. Miscounting endpoints is one of the most common traps.
💡 Think about this first

A game that rolled the same number every turn would be no fun, and a simulation that never varied would be useless. Programs create surprise with random values: a value the computer picks that you cannot predict in advance. You cannot say which number will come up, but you can say exactly which numbers are possible and how many there are. This lesson is about controlling that range with confidence.

Why Programs Need Randomness

Some programs must do something different each time they run. A dice game should not roll the same number every turn. A simulation of traffic or weather needs variety to be realistic. A quiz app that samples a question should not always pick the first one. For all of these, a program uses a random value: a value the computer chooses that the programmer cannot predict in advance.

In AP pseudocode you generate one with RANDOM(a, b). In Python the closest match is random.randint(a, b). Both return a random integer, and both include a and b as possible results.

import random
roll = random.randint(1, 6)
print(roll)
roll <- RANDOM(1, 6)
DISPLAY(roll)

Each time this runs, roll may be a different integer from 1 to 6. Run it three times and you might see 4, then 1, then 6. That unpredictability is the whole point. You cannot know the exact value ahead of time, but you do know the set of values it could be, and that is what the exam tests.

🎯 What the exam expects

The CED (AAP-3.E) states that RANDOM(a, b) "generates and returns a random integer from a to b, including a and b." The word to burn in is including: both endpoints are possible results. Python's random.randint(a, b) behaves the same way, inclusive of both a and b.

✍ Mini Exercise 1 • Count the outcomes
How many different integers could n be after this line?
n <- RANDOM(3, 9)
DISPLAY(n)

Counting the Possible Values

Because both endpoints are included, the number of possible results of RANDOM(a, b) is b - a + 1, not b - a. The + 1 is there because you are counting the fenceposts, not the gaps between them. From 2 to 8 the values are 2, 3, 4, 5, 6, 7, 8, which is seven numbers, and 8 - 2 + 1 equals 7.

lo = 2
hi = 8
count = hi - lo + 1
print(count)
lo <- 2
hi <- 8
count <- hi - lo + 1
DISPLAY(count)

Forgetting the + 1 is the single most common random-value mistake on the exam. If a question asks how many outcomes are possible, or asks you to make exactly a certain number of outcomes, reach for b - a + 1 every time.

⚠ Off by one

A range that looks like it has 6 values might have 5, or 7. RANDOM(1, 6) has 6 outcomes (1 through 6). RANDOM(0, 6) has 7 outcomes (0 through 6). RANDOM(5, 5) has exactly 1 outcome: it always returns 5. Always list or count the endpoints, then apply b - a + 1.

✍ Mini Exercise 2 • Spot the flaw
This is supposed to simulate a die roll, but it shows 6 every single time. What is wrong?
# meant to roll a die
roll = 6
print(roll)

Random, but Repeatable: Seeding

By design, random values change from run to run, which makes them hard to test. To get the same sequence every time, a program can seed the generator with random.seed(k) before drawing values. A seed does not remove the randomness; it just fixes which random sequence you get, so the same seed always produces the same results.

import random
random.seed(7)
print(random.randint(1, 6))
print(random.randint(1, 6))
DISPLAY(RANDOM(1, 6))
DISPLAY(RANDOM(1, 6))

With random.seed(7) set first, those two calls always print the same two numbers, in the same order, on every machine. That is why every practice problem below sets a seed: it makes the "random" output predictable so you can check your work. On the real exam and on the Create Task, you normally do not seed, because you want genuine variety.

🎯 Exam vs. practice

AP pseudocode has no seed function; RANDOM(a, b) is always freshly random. Seeding is a Python testing convenience. Understand it, but know that exam questions about RANDOM ask which values are possible, never which exact value appears.

Using a Random Value in a Decision

A random value becomes useful when the program acts on it. Generate the value, then feed it into a conditional to branch, or into a counter inside a loop to build a simulation. A coin flip is the classic example: draw 0 or 1, then decide.

import random
flip = random.randint(0, 1)
if flip == 1:
    print("heads")
else:
    print("tails")
flip <- RANDOM(0, 1)
IF (flip = 1)
{
    DISPLAY("heads")
}
ELSE
{
    DISPLAY("tails")
}

Here RANDOM(0, 1) returns one of two equally likely results. The IF turns that number into a word. This same pattern, generate a random value then branch on it, is how you would build a dice game, a random enemy spawn, or a sampled survey response in your own project.

✍ Mini Exercise 3 • Predict the seeded output
The seed is fixed, so this prints the same value every run. Type the integer it prints.
import random
random.seed(7)
print(random.randint(1, 6))
prints:

Key Vocabulary

Term Definition Example
Random value A value the program chooses unpredictably from a set of possibilities a die roll
RANDOM(a, b) AP pseudocode call that returns a random integer from a to b, including both RANDOM(1, 6)
random.randint(a, b) Python call that returns a random integer from a to b, inclusive of both random.randint(1, 6)
Inclusive range A range where both endpoints are possible results 1 and 6 both possible
Number of outcomes How many integers a call can return, equal to b - a + 1 6 - 1 + 1 = 6
Seed A fixed starting value that makes the random sequence repeatable random.seed(7)
📋 Create Performance Task • A random value that drives your program

Randomness is a natural way to make your Create Task program produce varied output and to show a meaningful use of an expression. A random value fed into a conditional or a loop lets one program behave differently each run, which is exactly the kind of behavior worth writing about in your response.

A point-earning example

Imagine a program that assigns a player a random target number and then reacts to it:

target <- RANDOM(1, 10) IF (target > 5) { DISPLAY("high target") } ELSE { DISPLAY("low target") }

In your written response, name the source of the value and describe the range and its use:

  • "The expression RANDOM(1, 10) generates a random integer from 1 to 10 inclusive, so there are ten equally likely outcomes."
  • "Because the value is unpredictable, the program produces different results on different runs, which the conditional then responds to."
  • "This use of a random value, combined with selection, lets the program handle varied input rather than a single fixed case."

The trap to avoid

Do not use RANDOM where you actually need a fixed value, and do not miscount the range. If your program is supposed to always start a score at 0, that is an assignment, not RANDOM(0, 0). And if you claim a call has a certain number of outcomes, count with b - a + 1 so your description is accurate. See the full Create Task module →

📈
MCQ Practice
6 questions • Exam difficulty and above • Predict before you peek
Question 1 of 6Trace
Predict first: list the smallest and largest values, then count.
A program runs random.randint(5, 12). How many different integer values could it return?
Incorrect. 7 is b - a, which forgets that both endpoints are included. The count is b - a + 1.
Correct. The values 5, 6, 7, 8, 9, 10, 11, 12 are eight integers, and 12 - 5 + 1 equals 8.
Incorrect. 12 is the largest value, not the number of possible values. Count from 5 to 12 inclusive.
Incorrect. 17 is 5 + 12. The count of outcomes is b - a + 1, which is 12 - 5 + 1.
Question 2 of 6Spot the bug
A designer wants a value that could be any integer from 1 to 6 inclusive, like a die. For which reason does this segment fail that goal?
import random x = random.randint(1, 5) print(x)
Correct. randint(1, 5) includes 5 but stops there, so 6 is impossible. To include 6 you must write randint(1, 6).
Incorrect. randint includes the first argument, so 1 is a possible result. The missing value is 6.
Incorrect. randint always returns an integer, never a decimal.
Incorrect. randint returns any value in the range with equal chance, not always the larger one.
Question 3 of 6Spot the bug
This code is supposed to start every new player with a score of exactly 0. Why is it wrong?
import random score = random.randint(0, 10) print(score)
Correct. A fixed starting value calls for a plain assignment, score = 0. randint(0, 10) makes the start unpredictable, which is the opposite of what was wanted.
Incorrect. randint(0, 10) includes 0, so 0 is one possible result, but the point is the value is not guaranteed.
Incorrect. Seeding only fixes which random sequence you get; it does not make the value always 0.
Incorrect. randint takes two arguments and this line is valid Python. The logic is the flaw, not the syntax.
Question 4 of 6I, II, III
Which of the following calls could return the value 10?
  • I. random.randint(1, 10)
  • II. random.randint(10, 20)
  • III. random.randint(11, 20)
Incorrect. I can return 10 as its upper endpoint, but II can also return 10 as its lower endpoint.
Incorrect. II can return 10, but so can I, since 10 is I's included upper endpoint.
Correct. I includes 10 as its upper bound and II includes 10 as its lower bound, both inclusive. III starts at 11, so 10 is impossible there.
Incorrect. III ranges from 11 to 20, so its smallest possible value is 11 and it can never return 10.
Question 5 of 6NOT question
A program uses random.randint(2, 6). Which value can it NOT return?
Incorrect. 2 is the lower endpoint and randint includes it, so 2 is possible.
Incorrect. 4 is between 2 and 6, so it is one of the possible results.
Incorrect. 6 is the upper endpoint and randint includes it, so 6 is possible.
Correct. The results range from 2 to 6 inclusive, so 7 is outside the range and can never occur.
Question 6 of 6Reasoning
Which single call returns a random integer with exactly four equally likely outcomes?
# choose the call with 4 possible results
Correct. randint(1, 4) can return 1, 2, 3, or 4, which is four outcomes, and 4 - 1 + 1 equals 4.
Incorrect. randint(0, 4) can return 0, 1, 2, 3, or 4, which is five outcomes, not four.
Incorrect. randint(1, 3) can return 1, 2, or 3, which is only three outcomes.
Incorrect. randint(4, 4) has just one outcome; it always returns 4.
🎮 Lesson Game
Random Ranger
Count the outcomes and read each seeded snippet to predict what it displays. 8 rounds.
0
Correct
1/8
Round
0
Streak 🔥
🐛 Python • how many, or which value?
0/8
correct answers
💻 Live Python Code Editor
Practice Problems
Real Python runs right here in your browser. The first time you press Run, the Python engine loads (a few seconds); after that it is instant. Problems build from guided to Create-Task level. Use Hint if you are stuck, and check your output against the target.
Hints used: 0 • Solutions viewed: 0
Level 1 • Guided
Problem 1 of 8 • one die roll
A seed is set so your result is repeatable. Print one random integer from 1 to 6 inclusive, like a single die roll. Target output: 3
Level 2 • Guided
Problem 2 of 8 • count the outcomes
The number of values RANDOM(a, b) can return is b - a + 1. Print how many integers RANDOM(2, 8) could return. Target output: 7
Level 3 • Range
Problem 3 of 8 • pick from 1 to 10
A seed is set. Print one random integer from 1 to 10 inclusive. Target output: 4
Level 4 • Fix the bug
Problem 4 of 8 • off by one
This should print how many values RANDOM(1, 6) can return, which is 6, but it prints the wrong number. Fix the count. Target output: 6
Level 5 • Decide
Problem 5 of 8 • seeded coin flip
A seed is set. Get a random value that is 0 or 1, then print a word:
  • if the value is 1: heads
  • otherwise: tails
Target output: tails
Level 6 • Create Task style
Problem 6 of 8 • random then branch
A seed is set. Generate a random integer from 1 to 100 inclusive, then classify it:
  • greater than 50: high
  • otherwise: low
Target output: high
Level 7 • Challenge
Problem 7 of 8 • you write the whole program
Open ended. Write the entire program yourself. A seed is set. Roll a 6-sided die (a random integer from 1 to 6 inclusive) 5 times, and print the sum of the five rolls. Only the seed is given. Target output: 16
Level 8 • Challenge
Problem 8 of 8 • count the results
Open ended. Write the entire program yourself. A seed is set. Flip a coin (a random 0 or 1) 10 times, and print how many times it came up heads (the value 1). Only the seed is given. Target output: 5

Frequently Asked Questions

Yes. RANDOM(a, b) returns a random integer from a to b including both endpoints. Python's random.randint(a, b) works the same way. So RANDOM(1, 6) can return 1, 2, 3, 4, 5, or 6.
Exactly b - a + 1. The plus one is there because both endpoints count. For example RANDOM(2, 8) can return 2, 3, 4, 5, 6, 7, or 8, which is 8 - 2 + 1 = 7 values.
A seed is a fixed starting value set with random.seed(k). It makes the random sequence repeatable, so the same seed always gives the same numbers. The practice problems seed the generator so the output is predictable and can be checked. On the real exam and on your Create Task you normally do not seed.
Use a fixed assignment when a value must always be the same, like starting a score at 0. Using RANDOM where a constant is needed makes the program unpredictable in a way you did not intend. Reach for randomness only when you truly want variety.
A random value fed into a conditional or a loop lets your program produce different results on different runs, which shows it handles varied situations. In your response, name the call, state the inclusive range, and describe how the program reacts to the value.
📦
AP CSP Teacher SuperpackSlides, lesson plans, unit tests for all 5 Big Ideas, $249
Get the Superpack →
🏫
For teachers

The Superpack includes an editable Topic 3.15 slide deck with animated inclusive-range number lines, a b - a + 1 counting bug bank, a print-ready random-values worksheet, and a unit quiz. View what's included →

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]