29.1 A Level Random Number Overview
- RANDOM() is the same built-in function at IGCSE (0478) and AS Level (9618) — it takes no parameters and returns a REAL between 0 and 1 inclusive.
- The function itself is unchanged; the step up at AS Level is in HOW you use it.
- AS Level questions expect you to combine RANDOM() with
INT(), arithmetic, arrays and loops. - Typical AS Level tasks: dice rolls, lottery numbers, coin tosses, random array selection, password generators and simulations.
Key idea: there is no built-in RANDOM(min, max) function in CIE pseudocode. To get a random integer in a range you must build it yourself from RANDOM() + INT() + arithmetic.
DECLARE R : REAL
R <- RANDOM()
OUTPUT R // e.g. 0.7234A Level Random Number Overview
29.2 The RANDOM Function (AS Level)
RANDOM() returns a random REAL number between 0 and 1 (inclusive). No parameters are needed.
DECLARE R : REAL
R <- RANDOM()
OUTPUT R // e.g. 0.7234Because RANDOM() returns a value, it can be used directly inside expressions:
DECLARE Scale : REAL
Scale <- RANDOM() * 100 // 0 to 100
OUTPUT Scale- No parameters: the parentheses are always empty.
- Return type: REAL (a decimal fraction, not an INTEGER).
- Range: closed interval [0.0, 1.0] — endpoints included.
- Each call returns a different (pseudo-random) value.
Exam tip: if a question asks for a random percentage, scale, or probability, reach for RANDOM() * N. The result is still a REAL — wrap with INT() only when you actually need an integer.
The RANDOM Function (AS Level)
29.3 Generating Random Integers in Ranges
To generate a random integer in the range [min, max] (both inclusive), use the canonical AS Level formula:
INT(RANDOM() * (max - min + 1)) + minExample — dice roll (1 to 6):
DECLARE Dice : INTEGER
Dice <- INT(RANDOM() * 6) + 1
OUTPUT DiceExample — random integer 10 to 20:
DECLARE Num : INTEGER
Num <- INT(RANDOM() * 11) + 10
OUTPUT NumRANDOM() * 11gives a REAL in [0, 10.999…].INT()floors to integers 0, 1, …, 10.+ 10shifts the range to 10, 11, …, 20.
Warning: always use INT() (floor), never ROUND(), when generating random integers. ROUND() rounds to the nearest integer, so the boundary values appear with about half the probability of the middle values — giving an unfair distribution.
Generating Random Integers in Ranges
29.4 Random with Arrays
Generate a random index to pick a random element from an array. The exact formula depends on whether the array is 1-indexed or 0-indexed.
Example — pick a random name from a 1-indexed array:
DECLARE Names : ARRAY[1:5] OF STRING
DECLARE Index : INTEGER
Names[1] <- "Alice"
Names[2] <- "Bob"
Names[3] <- "Charlie"
Names[4] <- "Diana"
Names[5] <- "Eve"
Index <- INT(RANDOM() * 5) + 1
OUTPUT "Random name: ", Names[Index]- For a 1-indexed array of size N:
INT(RANDOM() * N) + 1→ valid indices 1..N. - For a 0-indexed array of size N:
INT(RANDOM() * N)→ valid indices 0..N-1. - Useful for: games (random opponent), simulations (random selection), question banks.
Rule of thumb: the multiplier is always the array SIZE (number of elements). The +1 is added only when the array is 1-indexed. Getting this wrong leads to off-by-one or out-of-bounds errors.
Random with Arrays
29.5 Practical Applications
RANDOM() powers many real-world simulations and games. Here are five classic AS Level applications.
Example 1 — Dice game (roll 2 dice, sum)
DECLARE Dice1, Dice2, Sum : INTEGER
Dice1 <- INT(RANDOM() * 6) + 1
Dice2 <- INT(RANDOM() * 6) + 1
Sum <- Dice1 + Dice2
OUTPUT "You rolled: ", SumExample 2 — Coin toss
DECLARE Toss : INTEGER
Toss <- INT(RANDOM() * 2)
IF Toss = 0 THEN
OUTPUT "Heads"
ELSE
OUTPUT "Tails"
ENDIFExample 3 — Lottery (1–49)
DECLARE Num : INTEGER
Num <- INT(RANDOM() * 49) + 1
OUTPUT "Lottery: ", NumExample 4 — Random maths quiz
DECLARE A, B, Ans : INTEGER
A <- INT(RANDOM() * 10) + 1
B <- INT(RANDOM() * 10) + 1
OUTPUT A, " + ", B, " = ?"
INPUT AnsTrap: a lottery generator must produce 6 unique numbers. Calling INT(RANDOM() * 49) + 1 six times can give duplicates. You must check whether each new number is already in the list (using a flag or a WHILE loop) and re-roll if so.
Exam tip: when a question says "randomly select", "simulate" or "roll", reach for INT(RANDOM() * N) + min — the universal AS Level random-integer pattern.
Practical Applications
29.6 Key Points Summary
A quick recap of everything you need to remember about RANDOM() at AS Level.
Remember: RANDOM() returns a REAL in [0, 1]. Everything else — ranges, dice, indices, passwords — is built from INT(RANDOM() * size) + offset.