Recap
RANDOM(a, b)returns a random integer from a to b inclusive. Each value is equally likely.- Number of possible values: b − a + 1.
- Each execution may give a different result.
- To find the possible results of an expression, list each possible
RANDOMvalue and apply the expression. - A sum or other combination of two calls is not uniform.
- Probability of an event: favourable values ÷ number of possible values.
- Random list element:
aList[RANDOM(1, LENGTH(aList))].
Key relationships
| Goal | Expression | Possible values |
|---|---|---|
| Die roll | RANDOM(1, 6) |
1 to 6 |
| Coin flip | RANDOM(1, 2) (1 = heads) |
1 or 2 |
| Multiple of 5 from 10 to 50 | RANDOM(2, 10) * 5 |
10, 15, …, 50 (9 values) |
| Odd numbers 1, 3, 5 | 2 * RANDOM(1, 3) - 1 |
1, 3, 5 |
| 30% chance | RANDOM(1, 10) ≤ 3 |
true 3 times in 10 |
| Random list item | aList[RANDOM(1, LENGTH(aList))] |
Every element, no error |
Assumptions
- Each
RANDOMcall is independent of the others. - Every integer in the range is equally likely for a single call.
- Indices start at 1.
Method for possible-results questions
- Write the list of values each
RANDOMcall can return (count them: b − a + 1). - Substitute each value into the rest of the expression.
- Collect the set of results. Remove repeats.
- If asked about likelihood, count how many ways each result can happen.
Mistakes to avoid
- Forgetting the end values are included.
- Counting
b − apossible values. - Treating the sum of two calls as uniform.
- Using
LENGTH(aList) + 1or 0 as an index limit. - Judging a random program after one run.
Worked mini-example
Possible results of RANDOM(1, 4) * 3: the values 1, 2, 3, 4 become 3, 6, 9, 12. So the set is {3, 6, 9, 12}. Four values, each with probability 1/4. The result is never 4 or 5 or 10.
Quick self-check
- How many values can
RANDOM(5, 12)return? - What are the possible results of
RANDOM(1, 3) + 10? - What is the probability that
RANDOM(1, 5) > 3?
Answers
- 12 − 5 + 1 = 8.
- 11, 12 or 13.
- Values 4 and 5 qualify: 2 out of 5, which is 2/5 or 40%.
Where marks are won and lost
Typical questions ask which values an expression could produce, or which code could display a particular value. List the possible RANDOM values, apply the expression to each and read off the set. Another kind gives a conditional using RANDOM and asks the probability of a branch or the possible outputs. Look for off-by-one slips in ranges (inclusive ends) and in list indices. You may also be asked why a random program should be tested more than once.
Back to the full study guide for worked examples and explanations.