Topic 3.15: Random Values | AP CSP Big Idea 3 | APCSExamPrep.com
Random Values
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
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.
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.
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.
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.
# 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.
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.
import random random.seed(7) print(random.randint(1, 6))
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) |
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:
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 →
Get a free AP CSP question every day
Join 3,000+ students. Daily practice, study tips, and exam strategies.
random.randint(5, 12). How many different integer values could it return?10?- I. random.randint(1, 10)
- II. random.randint(10, 20)
- III. random.randint(11, 20)
random.randint(2, 6). Which value can it NOT return?RANDOM(a, b) can return is b - a + 1. Print how many integers RANDOM(2, 8) could return. Target output: 7
RANDOM(1, 6) can return, which is 6, but it prints the wrong number. Fix the count. Target output: 6
- if the value is 1:
heads - otherwise:
tails
- greater than 50:
high - otherwise:
low
Frequently Asked Questions
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.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.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.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.🔗 Continue studying
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.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]