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:
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.
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)
| Run | Displayed |
|---|---|
| 1 | 4 5 |
| 2 | 4 3 |
| 3 | 3 3 |
| 4 | 1 5 |
| 5 | 2 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 want | Expression | Possible values |
|---|---|---|
| a die roll | RANDOM(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 9 | RANDOM(0, 9) | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 |
| an even number from 2 to 10 | 2 * RANDOM(1, 5) | 2, 4, 6, 8, 10 |
| a multiple of 10 from 10 to 50 | 10 * RANDOM(1, 5) | 10, 20, 30, 40, 50 |
| a number from 20 to 25 | RANDOM(0, 5) + 20 | 20, 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:
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).
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
- Write down every value each
RANDOMcall can return. - Push that whole list through the arithmetic.
3 * RANDOM(1, 4)turns 1, 2, 3, 4 into 3, 6, 9, 12. - If there are two calls, combine every value of one with every value of the other.
- Follow any
IFstatements for each value. - Anything not in your final list can never be displayed.
6Worked examples
Multiples of three
Which of 3, 9, 10, and 12 could this segment display?
x ← 3 * RANDOM(1, 4) DISPLAY(x)
- 1
RANDOM(1, 4)returns 1, 2, 3, or 4. - 2Times 3: 3, 6, 9, 12.
Two calls added
List every value this segment could display.
x ← RANDOM(1, 3) y ← RANDOM(1, 3) DISPLAY(x + y)
- 1Each call returns 1, 2, or 3, so there are 3 × 3 = 9 combinations.
- 2The smallest total is 1 + 1 = 2; the largest is 3 + 3 = 6; every total in between can be made.
A prize or a number
What could this segment display?
n ← RANDOM(1, 10)
IF (n > 7)
{
DISPLAY("prize")
}
ELSE
{
DISPLAY(n)
}
- 1
nis one of 1 to 10. - 2For 8, 9, and 10 the condition
n > 7is true and the program displaysprize. - 3For 1 to 7 it displays the number.
prize. It can never display 8, 9, or 10.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
RANDOM(0, 8)has 9 values, including a 0 the spinner does not have. - 2
RANDOM(1, 7)has 7 values and can never land on 8. - 3
RANDOM(1, 8)has 8 equally likely values, one per section.
RANDOM(1, 8).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)
- 1The loop adds 0 or 1 three times.
- 2The total is the number of heads in three flips: anywhere from none to all three.
7Common mistakes
RANDOM(1, 6) includes 6. To get the digits 0 through 9, write RANDOM(0, 9), not RANDOM(0, 10) or RANDOM(1, 9).
RANDOM returns whole numbers only. RANDOM(1, 4) never returns 2.5.
RANDOM(2, 12) makes 12 as likely as 7. Two dice need two calls added together.
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.
“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.