← IGCSE Computer Science Paper 2

Random whole numbers with ROUND and RANDOM

Gives a random whole number from 0 to 6. RANDOM() gives a decimal from 0 to 1, times 6 stretches it, and ROUND(..., 0) turns it into a whole number.

Understanding Random whole numbers with ROUND and RANDOM in IGCSE Computer Science Paper 2

What it does: RANDOM() runs first inside the brackets, then its result is multiplied by 10 and passed to ROUND(), which makes a whole number from 0 to 10. Adding 1 moves the whole range up. 0 to 6 becomes 1 to 7. Multiplying by 1 changes nothing. A function must RETURN its value to the code that called it. Times 5 gives 0 to 5, and + 1 makes it 1 to 6, like a die. Multiplying by the wrong number makes the top of the range too large, so the die could roll an impossible 7.

Common mistakes: ROUND needs a second argument saying how many decimal places to keep. For a whole number it must be ROUND(RANDOM() * 6, 0). A function must pass its result back to the caller with RETURN. OUTPUT only displays the value, so line 4 should be RETURN N.

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 expression gives a random whole number from 0 to 6?
ROUND(RANDOM() * 6, 0)RANDOM() * 6
Matches
Value ← ROUND(RANDOM(), 0) * 6
↓
ROUND is applied before multiplying. RANDOM() is a decimal from 0 to 1, so it rounds to only 0 or 1, giving just 0 or 6. The multiplication should be inside ROUND: ROUND(RANDOM() * 6, 0).
Applying
Meanings
Term
ROUND(RANDOM() * 6, 0)
Meaning
Gives a random whole number from 0 to 6. RANDOM() gives a decimal from 0 to 1, times 6 stretches it, and ROUND(..., 0) turns it into a whole number.
Practice
FUNCTION RollDie() RETURNS INTEGER
Testing
Gaps
Value ← ROUND(() * 6, 0)
Check Answer
Spots
1DECLARE Roll : INTEGER
2Roll ← ROUND(RANDOM() * 6)

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

The 'Constellation Wolf' badge — Space Legend, 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 expression gives a random whole number from 0 to 6?
A. ROUND(RANDOM() * 6, 0) ✓
B. RANDOM() * 6
RANDOM() * 6 gives a decimal such as 3.72. ROUND(..., 0) turns it into a whole number.
2. What does RANDOM() give on its own?
A. A whole number from 1 to 6
B. A decimal from 0 to 1 ✓
RANDOM() gives a decimal from 0 to 1. You multiply it and round it to get whole numbers.
3. Which line stores a random whole number from 0 to 10?
A. Value ← ROUND(RANDOM() * 10, 0) ✓
B. Value ← ROUND(RANDOM(), 10)
Multiply RANDOM() by 10 to stretch the range. The 0 in ROUND(..., 0) is the number of decimal places.
4. In ROUND(RANDOM() * 6, 0), what does the 0 mean?
A. The smallest number that can be picked
B. Round to 0 decimal places, so a whole number ✓
The second value in ROUND is the number of decimal places. 0 decimal places gives a whole number.
5. Which expression gives a random whole number from 1 to 7?
A. ROUND(RANDOM() * 6, 0) + 1 ✓
B. ROUND(RANDOM() * 6, 0) * 1
Adding 1 moves the whole range up. 0 to 6 becomes 1 to 7. Multiplying by 1 changes nothing.

Match the Pairs

Value ← ROUND(RANDOM() * 6, 0) → Stores a random whole number from 0 to 6 in the variable Value
Roll ← ROUND(RANDOM() * 5, 0) + 1 → Stores a random whole number from 1 to 6, like a die roll
Nesting one routine inside another: RANDOM() is called inside ROUND() to get a whole number → ROUND(RANDOM() * 10, 0)
A function that returns a die roll from 1 to 6 → FUNCTION RollDie() RETURNS INTEGER RETURN ROUND(RANDOM() * 5, 0) + 1 ENDFUNCTION
Value ← ROUND(RANDOM(), 0) * 6 → ROUND is applied before multiplying. RANDOM() is a decimal from 0 to 1, so it rounds to only 0 or 1, giving just 0 or 6. The multiplication should be inside ROUND: ROUND(RANDOM() * 6, 0).
Roll ← ROUND(RANDOM() * 6, 0) + 1 → This gives 1 to 7, not 1 to 6, because the range 0 to 6 has 7 values and adding 1 shifts it to 7. To roll a die, multiply by 5 instead: ROUND(RANDOM() * 5, 0) + 1.

Key Term

ROUND(RANDOM() * 6, 0)
Gives a random whole number from 0 to 6. RANDOM() gives a decimal from 0 to 1, times 6 stretches it, and ROUND(..., 0) turns it into a whole number.

Fill in the Blanks

1. Value ← ROUND(RANDOM() * 6, 0)
Hint: Type the library routine that gives a random decimal from 0 to 1.
2. // random whole number 0 to 10 Value ← ROUND(RANDOM() * 10, 0)
Hint: Type the number to multiply by, which is the highest whole number the comment asks for.
3. // random whole number 1 to 6 Value ← ROUND(RANDOM() * 5, 0) + 1
Hint: Type the number added to move the range 0 to 5 up to the range in the comment.
4. FUNCTION RollDie() RETURNS INTEGER RETURN ROUND(RANDOM() * 5, 0) + 1 ENDFUNCTION
Hint: Type the number of decimal places that gives a whole number.
5. Value ← ROUND(RANDOM() * 6, 0)
Hint: Type the outer routine that wraps the random calculation and makes the result a whole number.

Spot the Error

Question 1
A DECLARE Roll : INTEGER
B Roll ← ROUND(RANDOM() * 6)
C OUTPUT Roll
Line B — ROUND needs a second argument saying how many decimal places to keep. For a whole number it must be ROUND(RANDOM() * 6, 0).
Question 2
A FUNCTION Die() RETURNS INTEGER
B DECLARE N : INTEGER
C N ← ROUND(RANDOM()*5, 0) + 1
D OUTPUT N
E ENDFUNCTION
Line D — A function must pass its result back to the caller with RETURN. OUTPUT only displays the value, so line 4 should be RETURN N.
Question 3
A DECLARE Dice : INTEGER
B Dice ← ROUND(RANDOM() * 5, 0)
C Dice ← Dice * 1
D OUTPUT Dice
Line C — To move the range from 0 to 5 up to 1 to 6 you must add 1. Multiplying by 1 changes nothing. It should be Dice ← Dice + 1.
Question 4
A Roll ← ROUND(RANDOM * 6, 0)
B Bonus ← Roll * 10
C OUTPUT Bonus
Line A — RANDOM is a library routine, so it needs brackets when it is called. It should be RANDOM(), giving ROUND(RANDOM() * 6, 0).
Question 5
A Num ← ROUND(RANDOM(), 0) * 10
B OUTPUT "Number: ", Num
C OUTPUT "Max is 10"
Line A — The multiplication must happen before rounding. Here RANDOM() is rounded first to 0 or 1, so the result is only 0 or 10. It should be ROUND(RANDOM() * 10, 0).

Student with a class code? Join your class →