---
title: "AP CSP 3.15: RANDOM(a,b) and Random Values"
description: "Review AP Computer Science Principles Topic 3.15, including RANDOM(a,b), inclusive endpoints, equally likely random integers, rng meaning, random value expressions, possible results, and how random number generation appears in AP CSP programs."
canonical: "https://fiveable.me/ap-comp-sci-p/unit-3/random-values/study-guide/8SZRS3ELTeNzsvZ3E1OF"
type: "study-guide"
subject: "AP Computer Science Principles"
unit: "Unit 3 – Algorithms & Programming Fundamentals"
lastUpdated: "2026-06-11"
---

# AP CSP 3.15: RANDOM(a,b) and Random Values

## Summary

Review AP Computer Science Principles Topic 3.15, including RANDOM(a,b), inclusive endpoints, equally likely random integers, rng meaning, random value expressions, possible results, and how random number generation appears in AP CSP programs.

## Guide

## 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](/ap-comp-sci-p/key-terms/range "fv-autolink") 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](/ap-comp-sci-p/key-terms/index "fv-autolink"), or a simulation event. On the exam, the key skill is usually tracing the possible results of a random [expression](/ap-comp-sci-p/key-terms/expression "fv-autolink"), not memorizing a specific programming language library.

Here's what the random generator looks like in [AP pseudocode](/ap-comp-sci-p/key-terms/ap-pseudocode "fv-autolink"):
`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.

![](https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-BY9uvRHSYeXU.png?alt=media&token=c40ab8ec-13e2-4cf1-ad5e-1f59b1dbd561)

...and here's its [equivalent](/ap-comp-sci-p/unit-3/developing-algorithms/study-guide/eFTUAVlAEU4XUX3MeQmF "fv-autolink") in Python: 

```
import random
c = random.randint(a,b)
```

Notice how you have to import the random [module](/ap-comp-sci-p/unit-1/program-design-development/study-guide/SsouN8LrhRWiQ5hevIV6 "fv-autolink") into your program in order to gain access to the random generator. Note: In AP CSP [pseudocode](/ap-comp-sci-p/key-terms/pseudocode "fv-autolink") 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](/ap-comp-sci-p/unit-3/data-abstraction/study-guide/kMMTClSiHohfiaHMGFFE "fv-autolink") 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](/ap-comp-sci-p/key-terms/syntax "fv-autolink"). The AP Exam Reference Sheet gives you `RANDOM(a, b)` directly.

## Vocabulary

- **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.
- **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.

## FAQs

### 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.

## Structured Data

```json
{"@context":"https://schema.org","@type":"FAQPage","inLanguage":"en","mainEntity":[{"@type":"Question","@id":"https://fiveable.me/ap-comp-sci-p/unit-3/random-values/study-guide/8SZRS3ELTeNzsvZ3E1OF#what-does-rng-mean-in-ap-csp","name":"What does RNG mean in AP CSP?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@type":"Question","@id":"https://fiveable.me/ap-comp-sci-p/unit-3/random-values/study-guide/8SZRS3ELTeNzsvZ3E1OF#how-does-randoma-b-work","name":"How does RANDOM(a, b) work?","acceptedAnswer":{"@type":"Answer","text":"RANDOM(a, b) returns an integer between a and b, including both endpoints. Each possible integer in that range is equally likely."}},{"@type":"Question","@id":"https://fiveable.me/ap-comp-sci-p/unit-3/random-values/study-guide/8SZRS3ELTeNzsvZ3E1OF#is-randoma-b-inclusive-in-ap-csp","name":"Is RANDOM(a, b) inclusive in AP CSP?","acceptedAnswer":{"@type":"Answer","text":"Yes. RANDOM(a, b) is inclusive, so RANDOM(1, 6) can return 1, 2, 3, 4, 5, or 6."}},{"@type":"Question","@id":"https://fiveable.me/ap-comp-sci-p/unit-3/random-values/study-guide/8SZRS3ELTeNzsvZ3E1OF#how-do-you-find-all-possible-results-of-a-random-expression","name":"How do you find all possible results of a random expression?","acceptedAnswer":{"@type":"Answer","text":"First list every possible output of the RANDOM call. Then apply any arithmetic or logic around it to each possible value."}},{"@type":"Question","@id":"https://fiveable.me/ap-comp-sci-p/unit-3/random-values/study-guide/8SZRS3ELTeNzsvZ3E1OF#why-do-programs-use-random-values","name":"Why do programs use random values?","acceptedAnswer":{"@type":"Answer","text":"Programs use random values for behavior that should vary across runs, such as games, simulations, sampling, and choosing from multiple possible outcomes."}},{"@type":"Question","@id":"https://fiveable.me/ap-comp-sci-p/unit-3/random-values/study-guide/8SZRS3ELTeNzsvZ3E1OF#what-is-a-common-ap-csp-mistake-with-random-values","name":"What is a common AP CSP mistake with random values?","acceptedAnswer":{"@type":"Answer","text":"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."}}]}
```
