01 / 21

Random Values
in Programming

Explore how computers generate unpredictable outputs using random number generators, and learn to harness randomness in your programs.

AP Computer Science Principles

What Are Random Values?

A random value is a number (or item) selected from a set of possibilities where each outcome is equally likely to be chosen. In computing, we use pseudorandom number generators (PRNGs) because true randomness is difficult for deterministic machines to produce.

Why Randomness Matters

Games
Dice rolls, card shuffling, enemy behavior, and procedural world generation all depend on random values.
Simulations
Monte Carlo methods model complex systems by running thousands of random trials to estimate outcomes.
Security
Encryption keys, passwords, and authentication tokens rely on high-quality random number generation.

Visualizing Uniform Distribution

When we generate a random integer in a range, each value has an equal probability of being selected. Over many trials, the outcomes form a uniform distribution.

Frequency Value 1 2 3 4 5 6 ideal RANDOM(1, 6) over 6000 trials

The RANDOM Function

The AP CSP Exam Reference Sheet defines one random procedure. It returns an integer at random, where each integer from a through b (inclusive) is equally likely.

RANDOM(a, b)
a
The lower bound (minimum possible value, inclusive).
b
The upper bound (maximum possible value, inclusive).

True Random vs Pseudorandom

True Random Pseudorandom
Source Physical phenomena (atmospheric noise, radioactive decay) Deterministic algorithm with a seed value
Reproducible? No Yes (same seed = same sequence)
Speed Slower Very fast
Used in AP CSP? Not directly Yes: RANDOM(a, b)

Counting Possible Values

Given RANDOM(a, b), the total number of possible values is:

1
RANDOM(1, 10): \( 10 - 1 + 1 = 10 \) possible values
2
RANDOM(5, 8): \( 8 - 5 + 1 = 4 \) possible values (5, 6, 7, 8)
3
RANDOM(0, 99): \( 99 - 0 + 1 = 100 \) possible values

How to Apply Random Values

1
Identify the range. Determine the minimum and maximum values you need.
2
Call RANDOM(a, b). The function returns one integer from a to b inclusive.
3
Store the result. Assign it to a variable so you can use it later in your program.
4
Use with expressions. Combine with arithmetic to shift or scale the range if needed.
5
Repeat if needed. Each call produces a new, independent random value.

Example 1: Coin Flip

Simulate a fair coin flip that displays "Heads" or "Tails" with equal probability.

flip ← RANDOM(1, 2)
IF (flip = 1)
  {
    DISPLAY("Heads")
  }
ELSE
  {
    DISPLAY("Tails")
  }

Example 2: Dice Roll Sum

Simulate rolling two six-sided dice and computing their sum.

die1 ← RANDOM(1, 6)
die2 ← RANDOM(1, 6)
total ← die1 + die2
DISPLAY(total)
die1 + die2 = 7 total

Example 3: Shifting the Range

What if you need a random even number between 2 and 10? Combine RANDOM with arithmetic.

result ← RANDOM(1, 5) * 2
// Possible values: 2, 4, 6, 8, 10
1
RANDOM(1, 5) gives: 1, 2, 3, 4, 5
2
Multiply by 2 gives: 2, 4, 6, 8, 10

Example 4: Random Selection from a List

Use RANDOM to pick a random element from a list by generating a random index.

colors ← ["red", "blue", "green", "yellow"]
index ← RANDOM(1, LENGTH(colors))
DISPLAY(colors[index])
"red" index 1 "blue" index 2 "green" index 3 "yellow" index 4

Independence of Random Calls

Each call to RANDOM is independent: the result of one call does not affect the result of any other call. This is a fundamental property.

x ← RANDOM(1, 6)
y ← RANDOM(1, 6)
// x and y may or may not be equal
// They are completely independent
✓ Correct Thinking
Both x and y can be any value from 1 to 6. They might be the same, or they might be different.
✕ Wrong Thinking
"If x is 3, then y can't also be 3." This is false: each call has no memory of previous calls.

Storing vs Calling Again

A critical distinction: storing a random value in a variable gives you the same value every time you use that variable. Calling RANDOM again gives a new value.

Stored Value
x ← RANDOM(1, 10)
DISPLAY(x)
DISPLAY(x)
// Same value twice
New Calls
DISPLAY(RANDOM(1, 10))
DISPLAY(RANDOM(1, 10))
// Likely different values

Common Mistakes

✕ Off-by-One Range
Forgetting that both endpoints are inclusive. RANDOM(1, 5) has 5 values, not 4.
✕ Assuming Dependence
Thinking that previous calls influence future ones. Each call is independent.
✕ Reusing vs Recalling
Using a variable (same value) when you meant to call RANDOM again (new value), or vice versa.

Summary of Key Concepts

Concept Key Detail Example
Syntax RANDOM(a, b) RANDOM(1, 6)
Range a to b, inclusive 1, 2, 3, 4, 5, 6
Count \( b - a + 1 \) \( 6 - 1 + 1 = 6 \)
Independence Each call is separate x and y can be equal
Storage Variable holds one value x is fixed once assigned
Scaling Multiply or add to shift RANDOM(1,5)*2: evens

Question 1 of 5

What are the possible values returned by RANDOM(3, 7)?

Question 2 of 5

Consider the following code:

x ← RANDOM(1, 4)
DISPLAY(x)
DISPLAY(x)

Which statement is true?

Question 3 of 5

How many different values can RANDOM(10, 25) return?

Question 4 of 5

Which code correctly generates a random multiple of 5 between 5 and 50 (inclusive)?

Question 5 of 5

Which value of result is most likely to be displayed?