Revision Notes
OxfordAQA IGCSE Computer Science (9210): Random number generation in a programming language – Revision Notes
Revision notes for OxfordAQA IGCSE Computer Science 9210 section 3.2.9: random integers, ranges, chances, simulations, common errors and a self-test.
- Subject
- Computer Science
- Level
- IGCSE
- Topic
- Topic 2 – Programming
- Author
- Marlbridge Academic Team
- Updated
- Reviewed by
- Harris Khan (what this means)
Aligned to OxfordAQA IGCSE Computer Science (9210), For teaching from September 2017, exams from May/June 2019. Official specification .
Syllabus page (what it covers and how it is assessed): OxfordAQA IGCSE Computer Science.
Syllabus points this page covers
9210
- 3.2.9 Random number generation in a programming language
Found an error? Report a correction.
Need help with this topic? Request a free trial class for IGCSE Computer Science (9210).
Condensed notes for section 3.2.9 Random number generation in a programming language of OxfordAQA International GCSE Computer Science (9210). Worked examples are in the random number generation study guide.
What the specification asks
- Be able to use random number generation within your own programs.
- Not required: how pseudo-random numbers are generated.
The function
RANDOM_INT(low, high): a random integer from low to high, both included, each value equally likely.
Each call gives a new value.
| Language | 1 to 6 inclusive |
|---|---|
| Pseudocode | RANDOM_INT(1, 6) |
| Python | random.randint(1, 6) (after import random) |
| C# | rnd.Next(1, 7) (upper value excluded) |
Ranges for common tasks
| Task | Call |
|---|---|
| Dice | RANDOM_INT(1, 6) |
| Coin (0 = heads, 1 = tails) | RANDOM_INT(0, 1) |
| Card value 1 to 13 | RANDOM_INT(1, 13) |
| Random array element | items[RANDOM_INT(0, LEN(items) - 1)] |
| Random capital letter | CODE_TO_CHAR(RANDOM_INT(65, 90)) |
| Random digit character | CODE_TO_CHAR(RANDOM_INT(48, 57)) |
| Number of the form 100, 200 … 900 | RANDOM_INT(1, 9) * 100 |
Chances
- n in 10 chance:
RANDOM_INT(1, 10) ≤ n. - 25% chance:
RANDOM_INT(1, 4) = 1(orRANDOM_INT(1, 100) ≤ 25). - Several outcomes: store one value, then use
IF ... ELSE IF ... ELSE.
Where to generate the value
| Situation | Generate… |
|---|---|
| A target that must stay the same during a game | Once, before the loop |
| A new dice roll every turn | Inside the loop |
| One value tested against several outcomes | Once, stored in a variable, before the selection |
Simulations
- Repeat a random event many times and count the results, often in an array of counters.
- Results vary from run to run; that is expected.
- Total of two dice =
RANDOM_INT(1, 6) + RANDOM_INT(1, 6), notRANDOM_INT(2, 12)(which wrongly makes every total equally likely).
Testing random programs
- Check every value is within range and that both end values can occur.
- Replace the random call with a fixed value temporarily to test the rest of the logic.
- In exam traces, use the random values the question gives, in order.
Mini trace with given random values
score ← 0
FOR turn ← 1 TO 4
d ← RANDOM_INT(1, 6)
IF d MOD 2 = 0 THEN
score ← score + d
ELSE
score ← score - 1
ENDIF
ENDFOR
OUTPUT score
If the random values are 5, 2, 6, 3:
| turn | d | even? | score |
|---|---|---|---|
| 1 | 5 | No | −1 |
| 2 | 2 | Yes | 1 |
| 3 | 6 | Yes | 7 |
| 4 | 3 | No | 6 |
Output: 6. In a trace, the question supplies the random values; use them in order, one per call.
Key terms
| Term | Meaning |
|---|---|
| Random number generation | Producing numbers that cannot be predicted, for use in a program |
| Range | The smallest and largest values that can be produced |
| Inclusive | Both end values of the range can be produced |
| Simulation | A program that models a real event, often repeated many times with random values |
| Pseudo-random | Generated by an algorithm so as to appear random (how it works is not required) |
Exam traps
- Off-by-one ranges: 0 to 6 is seven values; 1 to
LEN(a)misses index 0. - Regenerating a value that should stay fixed.
- Two calls in an
IF ... ELSE IFchain, testing different numbers. RANDOM_INT(2, 12)for two dice.- Language ranges: C#’s
Next(1, 7)stops at 6; do not copy that habit into pseudocode asRANDOM_INT(1, 7). - Explaining the algorithm behind random numbers: not required and not credited.
Self-test
- State the possible values of
RANDOM_INT(3, 7). - Write a call that simulates a 10-sided dice numbered 1 to 10.
- Write a call that picks a random index for an array of 20 elements.
- Write a condition that is true with a 1 in 5 chance.
- Explain why the target number in a guessing game is generated before the loop.
- Explain why
RANDOM_INT(2, 12)does not correctly simulate two dice. - Write a statement that sets
letterto a random lower-case letter ("a"is 97,"z"is 122). - A program runs
RANDOM_INT(1, 6)600 times. About how many sixes would you expect, and why will it vary? - What is wrong with
IF RANDOM_INT(1, 3) = 1 THEN ... ELSE IF RANDOM_INT(1, 3) = 2 THEN ...? - Give one way to test a program that uses random numbers.
- State the Python function for a random integer in a range, both ends included.
- Does the specification require you to explain how pseudo-random numbers are produced?
Answers:
- 3, 4, 5, 6 or 7.
RANDOM_INT(1, 10).RANDOM_INT(0, 19)(orRANDOM_INT(0, LEN(a) - 1)).RANDOM_INT(1, 5) = 1(any single value from 1 to 5).- So the number stays the same for every guess; generating it inside the loop would change it each time.
- It makes all totals equally likely, but with two dice some totals (such as 7) can be made in more ways than others (such as 2).
letter ← CODE_TO_CHAR(RANDOM_INT(97, 122)).- About 100, because each face has a 1 in 6 chance; each run is random, so the exact count changes.
- Each call produces a different random number, so the two tests are not checking the same value; store one value first.
- Any one: check all values are in range; check both end values occur; replace the call with a fixed value to test the rest of the code.
random.randint(a, b).- No; only using random number generation is required.
More resources on this topic
Related resources
-
Study guides
OxfordAQA IGCSE Computer Science (9210): Relational operations in a programming language
Study guide to OxfordAQA IGCSE Computer Science 9210 section 3.2.4: the six relational operators, exam symbols, boundaries and conditions in loops and IFs.
Computer Science · OxfordAQA · IGCSE
Read guide -
Practice questions
OxfordAQA IGCSE Computer Science (9210): Relational operations in a programming language – Practice Questions
Original OxfordAQA IGCSE Computer Science 9210 practice questions with answers on relational operators, boundary values, loop counts and selection chains.
Computer Science · OxfordAQA · IGCSE
Try questions -
Revision notes
OxfordAQA IGCSE Computer Science (9210): Relational operations in a programming language – Revision Notes
Revision notes for OxfordAQA IGCSE Computer Science 9210 section 3.2.4: relational operators, exam symbols, boundary wording, opposites, traps and a self-test.
Computer Science · OxfordAQA · IGCSE
Revise topic
Get free revision emails (optional)
Occasional emails with practice questions, worked explanations and links to free resources for the qualification and subjects you choose. No spam, and you can unsubscribe from any email. The free tools on this site never need an email.