5m left·0%
Reading Time: 5 min
Last Updated: March 12, 2026
Main Ideas: 4
Reading Time: 5 min
Last Updated: March 12, 2026
Main Ideas: 4

Topic 3.15 Notes – Random Values

Verified for 2027 AP® Computer Science Principles Exam
Read aloud
Random values let a program introduce chance. In AP CSP pseudocode, this is done with RANDOM(a, b), which generates an integer between two endpoints. You need to know exactly what values it can return, how it behaves inside expressions, and how randomness affects a program’s output.

What RANDOM(a, b) Does

On the AP Exam Reference Sheet, you’ll see:

RANDOM(a, b)

It generates and returns a random integer from a to b, inclusive.

Break that down:

  • Integer → no decimals, no fractions.
  • From a to b → starting at a, ending at b.
  • Inclusive → both a and b are possible.
  • Equally likely → every integer in the range has the same probability.

Example:

  • RANDOM(2, 5) → possible results are 2, 3, 4, 5
  • That’s 4 possible outcomes.
  • Each has probability 1/4.

If a = b, like RANDOM(7, 7), the only possible result is 7.

A helpful counting shortcut:

Number of possible values = b − a + 1

So for RANDOM(3, 9)
9 − 3 + 1 = 7 possible integers.

That little +1 is where people mess up. If you forget it, you leave out one endpoint.

Determining All Possible Results

When the quiz gives you a random expression, your job is to figure out every value it could produce.

Step 1: List the raw random values

Example:

RANDOM(4, 6) → {4, 5, 6}

Always write the full set before doing anything else.

Step 2: Apply the rest of the expression

Now suppose you see:

value ← RANDOM(4, 6) - 1

Start with the random results:

{4, 5, 6}

Subtract 1 from each:

{3, 4, 5}

Final possible results → {3, 4, 5}

Another one:

score ← RANDOM(1, 3) * 3

Random part → {1, 2, 3}
Multiply each by 3 → {3, 6, 9}

You don’t guess. You transform each possible value.

This is very common in multiple-choice questions. They love hiding RANDOM inside math.

Using RANDOM in Conditions

Random values often control decisions.

num ← RANDOM(1, 4)

IF (num ≤ 2)
{
   DISPLAY("Low")
}
ELSE
{
   DISPLAY("High")
}

Possible num values → {1, 2, 3, 4}

  • 1 or 2 → "Low"
  • 3 or 4 → "High"

Two numbers trigger each branch, so:

  • P("Low") = 2/4 = 1/2
  • P("High") = 2/4 = 1/2

Even though there are 4 integers, the outputs are grouped. The probabilities depend on how many integers lead to each outcome.

Students often assume different branches are automatically 50/50. They’re only equal if the number of triggering values is equal.

RANDOM Inside Loops

Each time RANDOM runs, it generates a new value.

REPEAT 3 TIMES
{
   DISPLAY(RANDOM(1, 2))
}

Each iteration independently produces 1 or 2.

Possible output sequences include:

1 1 1
1 2 1
2 2 2
2 1 2

Repetition is allowed. The same number can appear multiple times.

Every run of the program might look different.

Why Random Programs Produce Different Results

When a program uses RANDOM, its behavior becomes nondeterministic.

That means:

  • Same code
  • Same inputs
  • Different outputs across runs

You cannot predict the exact number that will be returned.

But you can determine:

  • The full set of possible results
  • The probability of each result
  • The possible execution paths

On the AP exam, you are never expected to predict one specific outcome. You are expected to reason about all valid outcomes.

Writing Expressions That Generate Random Values

You might be asked to create an expression that generates certain values.

Examples:

  • Generate a number from 10 to 15 → RANDOM(10, 15)
  • Generate a number from 0 to 4 → RANDOM(0, 4)
  • Generate an even number from 2 to 10:
    • Even numbers are {2, 4, 6, 8, 10}
    • One way:
    RANDOM(1, 5) * 2
    

    Because {1,2,3,4,5} × 2 → {2,4,6,8,10}

This is where understanding expressions really matters. You control the output set by adjusting the math around RANDOM.

Key Takeaways

RANDOM(a, b) returns an integer from a to b inclusive, and every integer is equally likely.
The number of possible results is b − a + 1, not just b − a.
When RANDOM is inside a larger expression, apply the math to each possible random value.
In conditionals, probabilities depend on how many integers lead to each branch.
Programs using RANDOM are nondeterministic, but you can always determine the full set of possible outcomes.

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse this website.

Notes

1 credit used · 5/5 remaining