Unit 6: Simulations · Simulating randomness

Article

RANDOM(a, b): numbers by chance

7 min read

You roll a die. You cannot say which number will come up, but you know it will be 1, 2, 3, 4, 5 or 6, and never 7. A computer can do the same. This article shows how a program makes a number by chance, and how to say exactly which numbers are possible.

RANDOM(a, b)

AP pseudocode has a procedure for chance: RANDOM(a, b). It gives back a random integer (a whole number) from a to b, including both a and b. Every number in that range is equally likely. So RANDOM(1, 6) works like a fair die.

AP pseudocode
DISPLAY(RANDOM(1, 6))

In Python the same job is done by random.randint(a, b). It also includes both ends. Python needs import random first, as you saw in the lesson on libraries. AP pseudocode has no import.

Runs in your browser

Run it five times. The number changes from run to run. This is what we mean by random: you cannot predict the next result from the last one. A computer makes these numbers with a clever formula, but for a program they behave like chance.

How many different values can RANDOM(a, b) give? Count both ends: b - a + 1. For RANDOM(5, 9) the values are 5, 6, 7, 8 and 9, which is 9 - 5 + 1 = 5 values, not 4.

Unpredictable value, predictable range

Each single result is unpredictable. But the list of possible results is not. Run this to see it. It rolls 1000 times and keeps the smallest and the largest.

Runs in your browser

You will see 1 6 every time. No roll was ever 0 or 7. So we say: a program that uses RANDOM cannot be fully predicted, but its possible outputs can be listed. This is exactly what the exam asks.

Check your understanding

Which list shows every value that RANDOM(3, 7) can return?

Listing the possible values

To list every value a code segment could show, find the smallest and largest result, then ask which numbers in between can really happen. Be careful: a * or a MOD can leave gaps.

CodePossible valuesWhy
RANDOM(1, 4)1, 2, 3, 4both ends included
RANDOM(1, 4) + 1011, 12, 13, 14every value moves up by 10
RANDOM(1, 4) * 22, 4, 6, 8doubling leaves gaps: 3 and 5 are not possible
RANDOM(0, 9) MOD 30, 1, 2the remainder when dividing by 3
RANDOM(3, 3)3only one number to choose from

Two calls give two numbers. Every time RANDOM runs, it picks again, so two calls can give different results.

CodePossible values
RANDOM(1, 6) + RANDOM(1, 6)2, 3, 4, ... 12 (eleven values)
x ← RANDOM(1, 6) then x + x2, 4, 6, 8, 10, 12 (six values)

In the second line, x keeps one number, and then it is used twice. In the first line there are two different rolls. The sums 2 to 12 are all possible in the first line, but they are not equally likely: 7 can be made six ways (1+6, 2+5, ...), and 2 only one way (1+1).

Check your understanding

Which of these values can be displayed by DISPLAY(RANDOM(1, 4) * 2)?

Worked example

What could this code display?

AP pseudocode
a ← RANDOM(1, 5)
b ← RANDOM(1, 5)
c ← a - b
DISPLAY(c)
  1. Both a and b can be any whole number from 1 to 5, and they are chosen separately.
  2. The smallest c is when a is smallest and b is largest: 1 - 5 = -4.
  3. The largest c is 5 - 1 = 4.
  4. Every whole number in between can happen (for example -3 is 1 - 4, and 0 is 3 - 3).

So c can be any integer from -4 to 4, which is 9 values. A display of 5 or -5 is not possible. If the second line were b ← a, then c would always be 0.

Common mistakes

  • Leaving out an end. RANDOM(1, 6) can give 6. Python's range(1, 6) stops before 6, but random.randint(1, 6) does not.
  • Counting b - a values. The count is b - a + 1.
  • Forgetting the gaps. RANDOM(1, 4) * 2 cannot give 5.
  • Thinking two calls give the same number. Each call picks again. Only a variable keeps a value.
  • "Random means anything can happen." The values are limited to the range.
  • "After a 6, a 6 is less likely." Each call is a fresh pick. It has no memory.

On the exam

Expect "Which of the following is NOT a possible output?", "Which of the following lists all possible values?" and "Can the output of this program be predicted?" Find the smallest and largest value, then check for gaps made by * or MOD. Remember that a program using RANDOM can give a different result each run, so the exact result cannot be predicted, but the possible results can.

Key words

Random number: a number picked by chance, so you cannot predict it.

Integer: a whole number, such as -2, 0 or 7.

RANDOM(a, b): gives a random integer from a to b, including both. All values are equally likely.

Equally likely: each possible value has the same chance.

Possible output: a result that can happen, even if it does not happen on this run.

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse, this site. All lessons and questions here are written by JrCodex.