The Stacks
BIG IDEA 3: ALGORITHMS AND PROGRAMMING · TOPIC 3.15

3.15 Random Values

One procedure, one rule: RANDOM(a, b) returns a random integer from a to b, inclusive. Nearly every question is about that inclusivity or about probability.

What you need to know

  • RANDOM(a, b) returns a random integer between a and b, inclusive. RANDOM(1, 6) simulates a die: 1, 2, 3, 4, 5, or 6.
  • Each call is independent. Calling it twice can give the same value or different values — the program can't predict which.
  • Number of possible outcomes = b − a + 1. RANDOM(1, 10) has 10 outcomes; RANDOM(0, 10) has 11.
  • Each outcome is equally likely. Probability of a specific value = 1 / (b − a + 1). Probability that RANDOM(1, 10) ≤ 3 is 3/10.
  • Random values are used in games, simulations (3.16), sampling, and testing.
  • To scale or shift randomness, combine with arithmetic: RANDOM(1, 6) + RANDOM(1, 6) simulates two dice (2–12, not uniformly distributed).
  • A program that uses random values will generally produce different output each time it runs — a question may ask which statements are possible rather than certain.

Worked example

x ← RANDOM(1, 4)
IF (x ≤ 1)
{
    DISPLAY("rare")
}
ELSE
{
    DISPLAY("common")
}

Possible values: 1, 2, 3, 4 (four outcomes). "rare" displays only when x = 1 → probability 1/4 = 25%. "common" displays 75% of the time.

Exam tip: Count outcomes as b − a + 1, then count how many satisfy the condition. Students forget that both endpoints are included and get 1/9 instead of 1/10. If a question describes wanting a value "from 1 to 100," the call is RANDOM(1, 100), not RANDOM(0, 100).

Going deeper

The nuance, edge cases, and connections that turn a 3 into a 5.

  • RANDOM(a, b) returns an integer from a to b inclusive, each with equal probability. The count of possible values is b − a + 1. RANDOM(1, 100) has 100 outcomes; RANDOM(0, 100) has 101.
  • Probability of a condition: count outcomes satisfying it, divide by total. RANDOM(1, 20) < 6 → outcomes 1–5, so 5/20 = 25%.
  • Combining random calls: two dice is RANDOM(1, 6) + RANDOM(1, 6), giving 2–12 but not uniformly — 7 is most likely. The exam may ask whether an expression produces each value equally often; sums of random values don't.
  • Scaling and shifting: if you only have RANDOM(1, 10) and need even numbers 2–20, use RANDOM(1, 10) * 2. If you need multiples of 5 from 0 to 45, (RANDOM(1, 10) - 1) * 5.
  • Random values make programs non-deterministic: the same program can produce different output each run. Questions about such programs ask what's possible or what must be true, not what "the" output is.
  • Random values are essential to simulations (3.16) — they model the variation and uncertainty in real processes — and to games, sampling, and testing with varied inputs.

Mistakes that cost points

  • Excluding an endpoint. Both a and b are possible results. RANDOM(1, 6) can return 6.
  • Counting outcomes as b − a. It's b − a + 1. Ten outcomes from 1 to 10, not nine.
  • Assuming a sum of random values is uniform. It isn't. Middle values are more likely.
  • Asking "what is the output" of a random program. There isn't one. The question will ask what's possible or what the probability is.

Practice questions

Written in the style of the real exam. Try each one before revealing the answer.

Q1 What is the probability that the following code displays "win"?
n ← RANDOM(1, 5)
IF (n > 3)
{
    DISPLAY("win")
}
  1. A 1/5
  2. B 2/5
  3. C 3/5
  4. D 1/2
Show answer

Answer: B. Outcomes 1–5 (five total). n > 3 is true for 4 and 5 → 2/5.

Q2 Which of the following code segments simulates flipping a fair coin, displaying "heads" or "tails" with equal probability?
  1. A IF (RANDOM(1, 2) = 1) { DISPLAY("heads") } ELSE { DISPLAY("tails") }
  2. B IF (RANDOM(0, 2) = 1) { DISPLAY("heads") } ELSE { DISPLAY("tails") }
  3. C IF (RANDOM(1, 3) = 1) { DISPLAY("heads") } ELSE { DISPLAY("tails") }
  4. D IF (RANDOM(1, 2) > 2) { DISPLAY("heads") } ELSE { DISPLAY("tails") }
Show answer

Answer: A. RANDOM(1, 2) has exactly two equally likely outcomes. B and C have three outcomes (not 50/50); D can never be true.

Key vocabulary

RANDOM(a, b)
returns a random integer from a to b, inclusive, each equally likely