← IGCSE Computer Science Paper 2

RANDOM: a random number between 0 and 1

A library routine with empty brackets that returns a random REAL number from 0 to 1 inclusive, a different value each time it is called. Store it, e.g. Num ← RANDOM()

Understanding RANDOM: a random number between 0 and 1 in IGCSE Computer Science Paper 2

What it does: Nothing goes inside the brackets of RANDOM() because it needs no input value. RANDOM() returns a value such as 0.37, which is a REAL, so the receiving variable must be REAL, not INTEGER. RANDOM takes no arguments, so the brackets are left empty. The result is a REAL number, so it usually has a decimal part. Each call produces a new random value, so the two results will usually differ. The variable goes on the left of the assignment arrow and the RANDOM() call goes on the right. The empty brackets are needed to call the routine. Without them it is not a valid call.

Common mistakes: RANDOM() returns a REAL number between 0 and 1, so the variable that stores it must be declared as REAL, not INTEGER. RANDOM() takes no arguments, so the brackets must be empty. It should be Chance ← RANDOM().

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 429–438) and word search (Modules 419–438).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which call correctly uses the RANDOM library routine?
Num ← RANDOM()Num ← RANDOM(10)
Matches
Num ← RANDOM(10)
↓
RANDOM takes no arguments, so a value must not be put inside the brackets. It should be RANDOM().
Applying
Meanings
Term
RANDOM()
Meaning
A library routine with empty brackets that returns a random REAL number from 0 to 1 inclusive, a different value each time it is called. Store it, e.g. Num ← RANDOM()
Practice
DECLARE Num : REAL
Testing
Gaps
Num ← ()
Check Answer
Spots
1DECLARE Num : INTEGER
2Num ← RANDOM()

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Seahorse' badge — Range Rider, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which call correctly uses the RANDOM library routine?
A. Num ← RANDOM(10)
B. Num ← RANDOM() ✓
RANDOM takes no arguments, so the brackets are left empty.
2. What range does RANDOM() return values in?
A. From 0 to 1 inclusive ✓
B. From 1 to 10 inclusive
RANDOM() gives a number from 0 to 1. To get other ranges you must combine it with other routines such as ROUND.
3. Which statement about the result of RANDOM() is true?
A. It is a REAL number, such as 0.37 ✓
B. It is always an INTEGER, either 0 or 1
The result is a REAL number, so it usually has a decimal part.
4. RANDOM() is called twice in a program. What should you expect?
A. A different value each time it is called ✓
B. The same value both times
Each call produces a new random value, so the two results will usually differ.
5. Which line stores a random number in a variable called Chance?
A. RANDOM() ← Chance
B. Chance ← RANDOM() ✓
The variable goes on the left of the assignment arrow and the RANDOM() call goes on the right.

Match the Pairs

RANDOM() → A library routine that returns a random REAL number between 0 and 1 inclusive, using empty brackets
Num ← RANDOM() → Calls RANDOM and stores the returned random REAL value in the variable Num
RANDOM takes no arguments, so its brackets are left empty when it is called → Value ← RANDOM()
The result of RANDOM is a REAL number, so the variable that stores it should be declared as REAL → DECLARE Chance : REAL
Num ← RANDOM(10) → RANDOM takes no arguments, so a value must not be put inside the brackets. It should be RANDOM().
Num ← RANDOM → The brackets are missing. RANDOM is a routine, so it must be called with empty brackets: RANDOM().

Key Term

RANDOM()
A library routine with empty brackets that returns a random REAL number from 0 to 1 inclusive, a different value each time it is called. Store it, e.g. Num ← RANDOM()

Fill in the Blanks

1. Num ← RANDOM()
Hint: Type the name of the library routine that gives a random number between 0 and 1.
2. DECLARE Num : REAL Num ← RANDOM() OUTPUT Num
Hint: Type the keyword that creates a variable before it is used.
3. Value ← RANDOM() OUTPUT Value
Hint: Type the variable name that stores the random number, so its value can be output.
4. Num ← RANDOM()
Hint: Type the assignment operator that stores the returned value in the variable.
5. DECLARE Chance : REAL Chance ← RANDOM() OUTPUT Chance
Hint: Type the data type of the value returned by RANDOM, which can include decimals.

Spot the Error

Question 1
A DECLARE Num : INTEGER
B Num ← RANDOM()
C OUTPUT Num
Line A — RANDOM() returns a REAL number between 0 and 1, so the variable that stores it must be declared as REAL, not INTEGER.
Question 2
A DECLARE Chance : REAL
B Chance ← RANDOM(10)
C OUTPUT Chance
Line B — RANDOM() takes no arguments, so the brackets must be empty. It should be Chance ← RANDOM().
Question 3
A DECLARE First, Second : REAL
B First ← RANDOM()
C OUTPUT First
D Second ← RANDOM
Line D — A routine call needs brackets, even when there are no arguments. It should be Second ← RANDOM().
Question 4
A DECLARE Num : REAL
B Num ← RANDOM()
C IF Num <= 1
D OUTPUT "In range"
E ENDIF
Line C — An IF statement must have THEN after its condition. It should be IF Num <= 1 THEN.
Question 5
A DECLARE Luck : REAL
B RANDOM() ← Luck
C OUTPUT Luck
Line B — The assignment arrow points from the value to the variable. The result of RANDOM() is stored with Luck ← RANDOM().

Student with a class code? Join your class →