AP Computer Science Principles · Study guide

Random Values

AP® Computer Science Principles · Big Idea 3 · Algorithms and Programming

Random Values

How RANDOM(a, b) works, how to build an expression that gives the values you want, and how to work out every value a random program could display.

1RANDOM(a, b)

The reference language has one source of randomness:

Reference language
RANDOM(a, b)

It returns a random integer (a whole number) from a to b, including both ends. Each result is equally likely. RANDOM(1, 3) can return 1, 2, or 3, and never 2.5.

The number of possible values is b − a + 1. RANDOM(1, 6) has 6 values; RANDOM(5, 14) has 14 − 5 + 1 = 10, and 14 is one of them. The “+ 1” is there because both ends count.

Figure 1. Each of the six values is equally likely, so over many runs each comes up about one time in six. The counts wobble a little because the values are random.

2Every run can be different

Using random numbers in a program means each run may produce a different result. Here is one program run five times:

a ← RANDOM(1, 6)
b ← RANDOM(1, 6)
DISPLAY(a)
DISPLAY(b)
RunDisplayed
14 5
24 3
33 3
41 5
52 1

None of these runs is wrong. That changes the question you ask about a random program: not “what does it display?” but “what could it display?” and “what can it never display?” It also changes testing: one run tells you very little, so a random program is run many times.

3Building the range you want

Start with RANDOM to get the right number of equally likely values, then use arithmetic to move or stretch them:

You wantExpressionPossible values
a die rollRANDOM(1, 6)1, 2, 3, 4, 5, 6
a coin flip (1 = heads, 2 = tails)RANDOM(1, 2)1, 2
a digit from 0 to 9RANDOM(0, 9)0, 1, 2, 3, 4, 5, 6, 7, 8, 9
an even number from 2 to 102 * RANDOM(1, 5)2, 4, 6, 8, 10
a multiple of 10 from 10 to 5010 * RANDOM(1, 5)10, 20, 30, 40, 50
a number from 20 to 25RANDOM(0, 5) + 2020, 21, 22, 23, 24, 25

Multiplying spreads the values apart (2 * RANDOM(1, 5) gives only even numbers), and adding slides the whole range (RANDOM(0, 5) + 20 starts at 20). Every value in the table was checked by running the expression thousands of times.

4Chances and choices

A chance. To make something happen 30% of the time, use a condition that is true for 3 of 10 equally likely values:

hits ← 0
REPEAT 10000 TIMES
{
  IF (RANDOM(1, 10) ≤ 3)
  {
    hits ← hits + 1
  }
}
DISPLAY(hits)

In one run of 10,000 tries the program displayed 2,990, close to 30% of 10,000. Any 3 of the 10 values would work; ≤ 3 is the easiest to read.

A random item from a list. List positions start at 1 and end at the list's length, so pick the position with RANDOM(1, LENGTH(list)):

songs ← ["Rain", "Lanterns", "Static", "Harbor"]
DISPLAY(songs[RANDOM(1, LENGTH(songs))])

This can display any of the four songs. Writing RANDOM(0, LENGTH(songs)) instead gives five possible positions, and position 0 does not exist: in 3,000 test runs, 637 stopped with an index error, about one run in five.

Same range, different chances. “Each result is equally likely” is true for one call. Add two calls together and the totals are no longer equally likely:

Figure 2. Both programs produce totals from 2 to 12. One call makes every total equally likely; adding two calls makes 7 the most common total and 2 and 12 the rarest, as with real dice.

Both expressions can return 2 through 12, but only the sum of two calls behaves like two real dice, where there are many ways to make 7 (1 + 6, 2 + 5, 3 + 4, …) and only one way to make 12 (6 + 6).

The key idea

RANDOM(a, b) returns one of the b − a + 1 integers from a to b, both ends included, each equally likely. Because a random program can display something different on each run, describe it by the set of values it could display.

5Working out possible outputs

  1. Write down every value each RANDOM call can return.
  2. Push that whole list through the arithmetic. 3 * RANDOM(1, 4) turns 1, 2, 3, 4 into 3, 6, 9, 12.
  3. If there are two calls, combine every value of one with every value of the other.
  4. Follow any IF statements for each value.
  5. Anything not in your final list can never be displayed.

6Worked examples

Worked Example 1 Possible values

Multiples of three

Which of 3, 9, 10, and 12 could this segment display?

x ← 3 * RANDOM(1, 4)
DISPLAY(x)
  1. 1
    RANDOM(1, 4) returns 1, 2, 3, or 4.
  2. 2
    Times 3: 3, 6, 9, 12.
Answer: 3, 9, and 12 are possible. 10 is not a multiple of 3, so it can never be displayed.
Worked Example 2 Possible values

Two calls added

List every value this segment could display.

x ← RANDOM(1, 3)
y ← RANDOM(1, 3)
DISPLAY(x + y)
  1. 1
    Each call returns 1, 2, or 3, so there are 3 × 3 = 9 combinations.
  2. 2
    The smallest total is 1 + 1 = 2; the largest is 3 + 3 = 6; every total in between can be made.
Answer: 2, 3, 4, 5, 6. They are not equally likely: 4 comes from three combinations, 2 from only one.
Worked Example 3 Possible values

A prize or a number

What could this segment display?

n ← RANDOM(1, 10)
IF (n > 7)
{
  DISPLAY("prize")
}
ELSE
{
  DISPLAY(n)
}
  1. 1
    n is one of 1 to 10.
  2. 2
    For 8, 9, and 10 the condition n > 7 is true and the program displays prize.
  3. 3
    For 1 to 7 it displays the number.
Answer: Any of 1 through 7, or prize. It can never display 8, 9, or 10.
Worked Example 4 Write the expression

A spinner with 8 sections

A game spinner has 8 equal sections numbered 1 to 8. Which expression models one spin: RANDOM(1, 8), RANDOM(0, 8), or RANDOM(1, 7)?

  1. 1
    RANDOM(0, 8) has 9 values, including a 0 the spinner does not have.
  2. 2
    RANDOM(1, 7) has 7 values and can never land on 8.
  3. 3
    RANDOM(1, 8) has 8 equally likely values, one per section.
Answer: RANDOM(1, 8).
Worked Example 5 Possible values

Three coin flips

Each RANDOM(0, 1) stands for a coin flip (1 = heads). What could this display, and what does it count?

total ← 0
REPEAT 3 TIMES
{
  total ← total + RANDOM(0, 1)
}
DISPLAY(total)
  1. 1
    The loop adds 0 or 1 three times.
  2. 2
    The total is the number of heads in three flips: anywhere from none to all three.
Answer: 0, 1, 2, 3: the number of heads in three flips.

7Common mistakes

Common Mistake 1 · Leaving out an end

RANDOM(1, 6) includes 6. To get the digits 0 through 9, write RANDOM(0, 9), not RANDOM(0, 10) or RANDOM(1, 9).

Common Mistake 2 · Expecting decimals

RANDOM returns whole numbers only. RANDOM(1, 4) never returns 2.5.

Common Mistake 3 · Treating one call as two dice

RANDOM(2, 12) makes 12 as likely as 7. Two dice need two calls added together.

Common Mistake 4 · Calling a different result a bug

A random program is allowed to display something new each run. Test it many times and check that every result is in the possible set.

Describe a random program's output like this

“RANDOM(1, 4) returns 1, 2, 3, or 4, each equally likely, so 3 * RANDOM(1, 4) can display 3, 6, 9, or 12. It cannot display 10, and it may display a different value each time it runs.”

List what the call can return, push it through the arithmetic, and name what is impossible.