Built-in Functions

AS Level

Random Numbers (AS Level)

Master the AS Level use of RANDOM() — the same function that returns a REAL between 0 and 1, combined with INT(), arithmetic and arrays to produce dice rolls, lottery numbers, coin tosses and random array selection.

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.7234

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.7234

Because 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.

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)) + min

Example — dice roll (1 to 6):

DECLARE Dice : INTEGER
Dice <- INT(RANDOM() * 6) + 1
OUTPUT Dice

Example — random integer 10 to 20:

DECLARE Num : INTEGER
Num <- INT(RANDOM() * 11) + 10
OUTPUT Num
  • RANDOM() * 11 gives a REAL in [0, 10.999…].
  • INT() floors to integers 0, 1, …, 10.
  • + 10 shifts 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.

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.

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: ", Sum

Example 2 — Coin toss

DECLARE Toss : INTEGER
Toss <- INT(RANDOM() * 2)
IF Toss = 0 THEN
  OUTPUT "Heads"
ELSE
  OUTPUT "Tails"
ENDIF

Example 3 — Lottery (1–49)

DECLARE Num : INTEGER
Num <- INT(RANDOM() * 49) + 1
OUTPUT "Lottery: ", Num

Example 4 — Random maths quiz

DECLARE A, B, Ans : INTEGER
A <- INT(RANDOM() * 10) + 1
B <- INT(RANDOM() * 10) + 1
OUTPUT A, " + ", B, " = ?"
INPUT Ans

Trap: 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.

29.6 Key Points Summary

A quick recap of everything you need to remember about RANDOM() at AS Level.

RANDOM() returns a REAL between 0 and 1 inclusive — same at IGCSE and AS Level.
RANDOM() takes NO parameters; the parentheses are always empty.
INT(RANDOM() * (max - min + 1)) + min gives a random integer in [min, max].
INT(RANDOM() * N) + 1 gives a valid 1-index for an array of size N.
INT(RANDOM() * N) gives a valid 0-index for a 0-indexed array of size N.
Always use INT() (floor), never ROUND(), when generating random integers.
Two dice sum: (INT(RANDOM()*6)+1) + (INT(RANDOM()*6)+1) gives 2..12.
Lottery needs UNIQUE numbers — extra logic to skip duplicates.
Coin toss: INT(RANDOM() * 2) gives 0 or 1 with equal probability.
Useful for: games, simulations, sampling, password generation, quizzes.

Remember: RANDOM() returns a REAL in [0, 1]. Everything else — ranges, dice, indices, passwords — is built from INT(RANDOM() * size) + offset.

Question Bank

Answer all questions, then press Submit Quiz to see your score.

0/11 answered

Question 1Multiple Choice

Which value can RANDOM() legally return at AS Level?

Question 2True / False

INT(RANDOM() * 6) + 1 generates integers in the range 1 to 6 inclusive.

Question 3Multiple Choice

DECLARE Num : INTEGER
Num <- INT(RANDOM() * 11) + 10
OUTPUT Num

Question 4Multiple Choice

An array A is declared as ARRAY[1:N] OF INTEGER. Which expression picks a random element from A?

Question 5True / False

RANDOM() takes an integer parameter to specify the upper bound of the range.

Question 6Multiple Choice

Which expression simulates a fair coin toss (0 = heads, 1 = tails)?

Question 7Multiple Choice

DECLARE Chars : ARRAY[1:26] OF CHAR
DECLARE Index : INTEGER
// ... Chars[1..26] filled with 'A'..'Z' ...
Index <- INT(RANDOM() * 26) + 1
OUTPUT Chars[Index]

Question 8True / False

ROUND() is preferred over INT() when generating random integers in a range because it gives a more uniform distribution.

Question 9Multiple Choice

What is the data type of the expression RANDOM() * 100?

Question 10Multiple Choice

DECLARE LotteryNum : INTEGER
LotteryNum <- INT(RANDOM() * 49) + 1
OUTPUT LotteryNum

Question 11True / False

To generate 6 unique lottery numbers you need extra logic to check whether each new number has already been picked.

Answer all 11 questions to enable submission.