Random Values
in Programming
Explore how computers generate unpredictable outputs using random number generators, and learn to harness randomness in your programs.
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
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.
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.
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:
How to Apply Random Values
Example 1: Coin Flip
Simulate a fair coin flip that displays "Heads" or "Tails" with equal probability.
IF (flip = 1)
{
DISPLAY("Heads")
}
ELSE
{
DISPLAY("Tails")
}
Example 2: Dice Roll Sum
Simulate rolling two six-sided dice and computing their sum.
die2 ← RANDOM(1, 6)
total ← die1 + die2
DISPLAY(total)
Example 3: Shifting the Range
What if you need a random even number between 2 and 10? Combine RANDOM with arithmetic.
// Possible values: 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.
index ← RANDOM(1, LENGTH(colors))
DISPLAY(colors[index])
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.
y ← RANDOM(1, 6)
// x and y may or may not be equal
// They are completely independent
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.
DISPLAY(x)
DISPLAY(x)
// Same value twice
DISPLAY(RANDOM(1, 10))
// Likely different values
Common Mistakes
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
Consider the following program:
a ← RANDOM(1, 3)
b ← RANDOM(1, 3)
result ← a + b
DISPLAY(result)
Which value of result is most likely to be displayed?