Skip to main content

Unit 3 · Topic 3.15

3.15 Random Values

RANDOM lets a program produce unpredictable values, which games and simulations rely on. This topic covers exactly what RANDOM(a, b) can return, how to write random expressions for a given chance, and how to reason about every possible result.

Key terms

  • RANDOM(a, b)
  • inclusive range
  • random number generation
  • possible outcomes

RANDOM(a, b)

RANDOM(a, b) returns a random whole number from a to b, including both a and b. Each value in that range is equally likely. RANDOM(1, 6) acts like rolling one die: it can return 1, 2, 3, 4, 5 or 6.

The number of possible values is b − a + 1. RANDOM(5, 9) has 5 possible results, not 4.

Because of randomness, each run of a program may produce a different result. That's why questions ask what could be displayed, or which result is impossible, rather than what will be displayed.

Writing a random chance

To make something happen with a given probability, compare a RANDOM call with a cutoff:

GoalExpression
50% chanceRANDOM(1, 2) = 1
25% chanceRANDOM(1, 4) = 1
30% chanceRANDOM(1, 10) ≤ 3
70% chanceRANDOM(1, 10) ≤ 7
1 in 100 chanceRANDOM(1, 100) = 1

Combining random values

Adding two random values doesn't give an equally likely range. RANDOM(1, 6) + RANDOM(1, 6) can be 2 through 12, but 7 is much more likely than 2, because many pairs add to 7 (1 + 6, 2 + 5 and so on) while only one pair adds to 2. RANDOM(2, 12) also covers 2 through 12, but with every total equally likely, so it doesn't behave like two dice.

When a random call is stored in a variable, its value stays fixed until it's assigned again. x ← RANDOM(1, 3) then DISPLAY(x) twice shows the same number twice. Calling RANDOM twice can give two different numbers.

Picking a random list element

A common pattern picks a random element from a list: pick ← names[RANDOM(1, LENGTH(names))]. Because RANDOM includes both ends and lists start at 1, this always gives a valid index, and each element is equally likely.

names ← ["Ada", "Lin", "Sam"] pick ← names[RANDOM(1, LENGTH(names))] DISPLAY(pick)

This can display Ada, Lin or Sam. Writing RANDOM(0, LENGTH(names)) instead would sometimes produce index 0 and stop the program with an error.

Reasoning about all outcomes

For "which could be displayed" questions, work out the smallest and largest possible results, then check whether every value in between is actually reachable. For "which can never be displayed," look for a value outside the range, or one the code can't produce.

Randomness also makes testing harder, since one run may not show a bug. Programmers run such code many times and check that every result is in the allowed range.

Worked examples

Try each one yourself first, then open the solution.

  1. Example 1

    Which values are possible?

    What values could this code segment display?x ← RANDOM(1, 3) y ← RANDOM(x, 4) DISPLAY(y - x)

    Show the solution
    1. Step 1: x can be 1, 2 or 3. Then y is between x and 4, inclusive, so y is never less than x.
    2. Step 2: The smallest value of y − x is 0 (when y = x).
    3. Step 3: The largest happens when x is as small and y as large as possible: x = 1, y = 4 gives 3.
    4. Step 4: Every value in between is reachable: x = 1 with y = 2 gives 1, and x = 1 with y = 3 gives 2.

    Answer: 0, 1, 2 or 3

  2. Example 2

    Matching a probability

    A game should give a bonus 25% of the time. Which condition does that: RANDOM(1, 4) = 1, RANDOM(0, 4) = 0 or RANDOM(1, 100) < 25?

    Show the solution
    1. Step 1: RANDOM(1, 4) = 1: four equally likely values, one of which works. 1 out of 4 is 25%. Correct.
    2. Step 2: RANDOM(0, 4) = 0: five values (0 to 4), so the chance is 1 out of 5, or 20%.
    3. Step 3: RANDOM(1, 100) < 25: the values 1 to 24 work, which is 24 out of 100, or 24%. It would need ≤ 25.

    Answer: RANDOM(1, 4) = 1

Common mistakes

  • Forgetting RANDOM includes both ends. RANDOM(1, 10) can return 10.
  • Counting the number of possible values as b − a instead of b − a + 1.
  • Assuming a sum of random values is equally likely across its range.

On the exam

  • Expect questions asking which values are possible or impossible, and which expression gives a stated probability. Check both endpoints and count the values.
  • RANDOM often appears inside simulations (3.16). Read whether the value is stored once in a variable or generated fresh each time.

Connected topics

Videos

Check yourself

4 questions on 3.15 Random Values. Pick an answer to see if you got it, and why.

Question 1 of 4

Which of the following could NOT be displayed by the statement below? DISPLAY(RANDOM(1, 6) + RANDOM(1, 6))

Question 2 of 4

A game should award a bonus with a 25% chance each turn. Which expression evaluates to true with a probability of exactly 25%?

Question 3 of 4

Which two of the following values could be displayed when the code segment below is run? Select two answers. x ← RANDOM(1, 3) y ← RANDOM(x, 5) DISPLAY(y - x)

Select two answers. 0 of 2 chosen

Question 4 of 4

A program should pick a random whole number from 10 to 20, where each of the 11 numbers from 10 through 20 is equally likely. Which expression does this?

0 of 4 answered