Article
Making something happen with a given chance
In a game, every time you open a chest there is a 30% chance of a rare gem. How does a program make something happen "30% of the time"? It uses RANDOM, and it counts. This article shows the counting, and the small traps that cost marks.
Turn a chance into a range of numbers
RANDOM(1, 100) picks one of 100 equally likely numbers. If the gem should appear 30% of the time, 30 of those 100 numbers must mean "gem". Any 30 will do. The simplest are 1 to 30:
IF(RANDOM(1, 100) ≤ 30)
{
DISPLAY("Gem!")
}
ELSE
{
DISPLAY("Nothing")
}Run it a few times. Most runs show Nothing, and about three in ten show Gem!.
The rule is always the same:
chance = (numbers that make the condition true) ÷ (all possible numbers)
| Goal | Code | Count |
|---|---|---|
| A die shows 6 (1 in 6) | RANDOM(1, 6) = 6 | 1 of 6 |
| Heads on a coin (50%) | RANDOM(1, 2) = 1 | 1 of 2 |
| 25% | RANDOM(1, 4) = 1 | 1 of 4 |
| 75% | RANDOM(1, 4) ≠ 1 | 3 of 4 |
| An even number on a die | RANDOM(1, 6) MOD 2 = 0 | 3 of 6 (2, 4, 6) |
Many different codes give the same chance. All of these are 75%: RANDOM(1, 100) ≤ 75, RANDOM(1, 100) > 25, RANDOM(1, 4) ≤ 3 and RANDOM(1, 8) ≤ 6.
Check your understanding
Which condition is true 20% of the time?
Off-by-one traps
The exam likes the little differences between <, ≤, > and ≥. Do not guess. List the numbers. Here, RANDOM(1, 100) has 100 values:
| Condition | True for | How many | Chance |
|---|---|---|---|
≤ 30 | 1 to 30 | 30 | 30% |
< 30 | 1 to 29 | 29 | 29% |
> 30 | 31 to 100 | 70 | 70% |
≥ 30 | 30 to 100 | 71 | 71% |
> 70 | 71 to 100 | 30 | 30% |
= 30 | 30 only | 1 | 1% |
Also count the total carefully. RANDOM(0, 99) has 100 values, so < 30 is 0 to 29, which is 30 values: 30%. But RANDOM(0, 100) has 101 values, which is not a nice number to work with.
Check your understanding
What is the chance that RANDOM(1, 100) > 40 is true?
Worked example: which code gives 25%?
Which of these IF conditions is true 25% of the time?
| Condition | Numbers that count | Total | Chance |
|---|---|---|---|
RANDOM(1, 25) = 1 | 1 | 25 | 4% |
RANDOM(1, 4) ≤ 1 | 1 | 4 | 25% |
RANDOM(1, 100) < 25 | 1 to 24 = 24 | 100 | 24% |
RANDOM(0, 100) ≤ 25 | 0 to 25 = 26 | 101 | about 25.7% |
Only the second row is exactly 25%. The third and fourth are near misses.
One pick, many outcomes
A spinner is red 50% of the time, blue 30% and green 20%. Use 10 numbers: 1 to 5 mean red (5 of 10), 6 to 8 blue (3 of 10), 9 and 10 green (2 of 10). Pick the number once, keep it in a variable, and test the variable.
r ← RANDOM(1, 10)
IF(r ≤ 5)
{
DISPLAY("red")
}
ELSE
{
IF(r ≤ 8)
{
DISPLAY("blue")
}
ELSE
{
DISPLAY("green")
}
}If r is 7, the first test is false, the second is true, and it displays blue. Now see the wrong way. If you write RANDOM(1, 10) again in each test, every IF makes a new pick. Then blue needs "not red" (50%) and a fresh pick of 8 or less (80%), which is 40%, and green is only 10%. The chances are wrong.
Run it many times to estimate a chance
A simulation can estimate a chance by running it many times and counting. This is useful when the counting is too hard to do by hand.
The number of gems is close to 300, nearly always between 250 and 350, and rarely exactly 300. Change trials to 100 and run it a few times: the percent jumps around. Change it to 100000: the percent stays very close to 30. More trials give a result closer to the true chance, but a short run can be far from it.
Common mistakes
- Using
<when you need≤.< 30onRANDOM(1, 100)is 29%. - Counting the wrong total.
RANDOM(0, 100)has 101 values, andRANDOM(1, 100)has 100. - A new
RANDOMin every test. Store the pick in a variable and test the variable. - "1 in 6 means exactly one 6 in every 6 rolls." It is a chance. A short run can have no 6 at all.
- "A rare event cannot happen twice in a row." Each pick is new. There is no memory.
On the exam
You will be shown several code segments and asked which one makes something happen with a given chance (such as 25%, or 1 in 10), or the chance of an event for a given segment. For each choice, count the values that make the condition true, count the total, and divide. Check both ends of every range.
Key words
Chance (probability): how likely something is, as a fraction or a percent.
Trial: one run of a simulation.
Estimate: an answer found by running many trials, close to the true chance but not exact.