Skip to main content

3.15 Random Values

add to ChatGPT
Verified for the 2027 examUpdated August 2026Fiveable Content Team
Fiveable

⌨️AP Computer Science Principles Unit 3 Review

QR code for AP Computer Science Principles practice questions

3.15 Random Values

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.

Random Values 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.

Practice Questions for 3.15

1 of 10

The flowchart above represents a single turn in a board game.

If a player starts their turn with a score of 10, what are all the possible values of score at the end of the turn?

A.

11 and 12

B.

8 and 12

C.

9, 10, 11, and 12

D.

9 and 12

Vocabulary

The following words are mentioned explicitly in the AP® course framework 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.

See Fiveable more often in Google

Mark Fiveable as a preferred source and Google is more likely to show you our guides in Top Stories, with a "preferred" badge in AI Mode and AI Overviews.

Prefer us on Google