Fiveable

⌨️AP Computer Science Principles Unit 3 Review

QR code for AP Computer Science Principles practice questions

3.15 Random Values

3.15 Random Values

Written by the Fiveable Content Team • Last updated June 2026
Verified for the 2027 exam
Verified for the 2027 examWritten by the Fiveable Content Team • Last updated June 2026
⌨️AP Computer Science Principles
Unit & Topic Study Guides

AP Computer Science Principles Exam

Pep mascot

Generating Random Numbers

In AP CSP, random values come from RANDOM(a, b), which returns an integer from a through b, including both endpoints. Each possible integer in that range is equally likely, so RANDOM(1, 3) can return 1, 2, or 3.

Random number generation is useful when a program needs behavior that can change from one run to the next, such as a dice roll, a random index, or a simulation event. On the exam, the key skill is usually tracing the possible results of a random expression, not memorizing a specific programming language library.

Here's what the random generator looks like in AP pseudocode: RANDOM(a, b) returns an integer value. RANDOM(a, b) returns a random integer from a to b, inclusive. Example: x <- RANDOM(1,6) can produce 1, 2, 3, 4, 5, or 6.

...and here's its equivalent in Python:

</>Code
import random
c = random.randint(a,b)

Notice how you have to import the random module into your program in order to gain access to the random generator. Note: In AP CSP pseudocode on the Exam Reference Sheet, RANDOM(a, b) is available without any import.

The Python example above will generate a random integer from a to b, inclusive. For example, c = random.randint(0,5) could result in the value of c being 0, 1, 2, 3, 4, or 5. In AP CSP pseudocode, each integer in the range a to b is equally likely to occur. For example, RANDOM(1,3) can return 1, 2, or 3 with equal probability.

Write and evaluate expressions

Use AP pseudocode expressions to produce specific ranges or sets, and list the possible results:

  • Die roll: r <- RANDOM(1,6) // results: 1, 2, 3, 4, 5, 6
  • Zero-based index: i <- RANDOM(0,9) // results: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
  • Even numbers 0..8: e <- 2 * RANDOM(0,4) // results: 0, 2, 4, 6, 8
  • Shifted range: s <- RANDOM(2,7) - 1 // results: 1, 2, 3, 4, 5, 6

Using random number generation in a program means that we might get a different result every time we run the program.

How to Use This on the AP CSP Exam

Random value questions usually ask you to determine every possible result of an expression. First list the outputs of RANDOM(a, b), then apply any arithmetic outside the random call. Remember that both endpoints are included.

Common traps:

  • Treating RANDOM(1,6) like it can return 0 or 7. It cannot.
  • Forgetting that each integer in the range is equally likely.
  • Evaluating only one possible run of the program when the question asks for all possible outputs.
  • Mixing up AP pseudocode with Python or JavaScript syntax. The AP Exam Reference Sheet gives you RANDOM(a, b) directly.

Vocabulary

The following words are mentioned explicitly in the College Board Course and Exam Description for this topic.

Term

Definition

random integer

A whole number selected randomly from a specified range, where each integer in that range has an equal probability of being chosen.

random number generation

The process of using a program function to produce unpredictable numeric values, where each program execution may produce different results.

random values

Numbers generated by a program where each possible outcome is equally likely to occur, and the specific result cannot be predicted in advance.

RANDOM(a, b)

A function that generates and returns a random integer from a to b, inclusive, where each result is equally likely to occur.

Frequently Asked Questions

What does RNG mean in AP CSP?

RNG means random number generator. In AP CSP pseudocode, random values are generated with RANDOM(a, b), which returns a random integer from a through b.

How does RANDOM(a, b) work?

RANDOM(a, b) returns an integer between a and b, including both endpoints. Each possible integer in that range is equally likely.

Is RANDOM(a, b) inclusive in AP CSP?

Yes. RANDOM(a, b) is inclusive, so RANDOM(1, 6) can return 1, 2, 3, 4, 5, or 6.

How do you find all possible results of a random expression?

First list every possible output of the RANDOM call. Then apply any arithmetic or logic around it to each possible value.

Why do programs use random values?

Programs use random values for behavior that should vary across runs, such as games, simulations, sampling, and choosing from multiple possible outcomes.

What is a common AP CSP mistake with random values?

A common mistake is forgetting the endpoints. Another is giving only one possible output when the question asks for all possible outputs of a random expression.

Pep mascot
Upgrade your Fiveable account to print any study guide

Download study guides as beautiful PDFs See example

Print or share PDFs with your students

Always prints our latest, updated content

Mark up and annotate as you study

Click below to go to billing portal → update your plan → choose Yearly→ and select "Fiveable Share Plan". Only pay the difference

Plan is open to all students, teachers, parents, etc
Pep mascot
Upgrade your Fiveable account to export vocabulary

Download study guides as beautiful PDFs See example

Print or share PDFs with your students

Always prints our latest, updated content

Mark up and annotate as you study

Plan is open to all students, teachers, parents, etc
report an error
description

screenshots help us find and fix the issue faster (optional)

add screenshot