Built-in Functions

Round & Random (IGCSE)

Master the two CIE IGCSE built-in functions: ROUND() for rounding a number to a given number of decimal places, and RANDOM() for generating a random REAL between 0 and 1.

28.1 What are Built-in Functions?

  • Built-in functions are pre-defined functions provided by the pseudocode language.
  • They perform common operations without needing to write custom code.
  • IGCSE level built-in functions include ROUND() and RANDOM().
  • They RETURN a value — so they can be used in expressions or assigned to a variable.
  • Syntax: Result <- FUNCTION(parameters)

Key idea: A function returns a value — that is what makes it different from a statement like OUTPUT. Always assign the result with <-.

DECLARE Result : REAL
Result <- ROUND(3.14159, 2)
OUTPUT Result   // Outputs: 3.14

28.2 The ROUND Function

ROUND(value, decimalPlaces) rounds a number to a specified number of decimal places and returns a REAL value.

DECLARE Price : REAL
DECLARE RoundedPrice : REAL
Price <- 19.987
RoundedPrice <- ROUND(Price, 2)
OUTPUT RoundedPrice   // Outputs: 19.99

More ROUND examples

  • ROUND(3.14159, 2) returns 3.14
  • ROUND(3.14159, 4) returns 3.1416
  • ROUND(7.5, 0) returns 8
  • ROUND(1234.5678, 1) returns 1234.6

Common uses: rounding currency to 2 decimal places, rounding measurements, displaying results without long decimal tails.

28.3 The RANDOM Function

RANDOM() generates a random REAL number between 0 and 1 (inclusive). It takes no parameters and returns a REAL value such as 0.347, 0.892 or 0.001.

DECLARE R : REAL
R <- RANDOM()
OUTPUT R   // Outputs something like: 0.4231

Combine with ROUND + arithmetic for random integers

  • Random 0–9: ROUND(RANDOM() * 9, 0)
  • Random 1–10: ROUND(RANDOM() * 9, 0) + 1
  • Random 1–100: ROUND(RANDOM() * 99, 0) + 1
  • Random 1–6 (dice): ROUND(RANDOM() * 5, 0) + 1

Watch out: RANDOM() takes no parameters — writing RANDOM(6) is wrong. To get a 1–6 dice value you must use the arithmetic pattern above.

28.4 Generating Random Integers

RANDOM() gives a value between 0 and 1, but we often need whole numbers in a specific range. Two equivalent formulas:

Using INT

INT(RANDOM() * (max - min + 1)) + min

Using ROUND

ROUND(RANDOM() * (max - min - 1), 0) + min

Example: Dice roll (1–6)

DECLARE DiceRoll : INTEGER
DiceRoll <- ROUND(RANDOM() * 5, 0) + 1
OUTPUT "You rolled: ", DiceRoll

Example: Random lottery number (1–49)

DECLARE LotteryNum : INTEGER
LotteryNum <- ROUND(RANDOM() * 48, 0) + 1
OUTPUT "Lottery number: ", LotteryNum

Rule of thumb: To get the multiplier for a range [min, max], use (max - min) with ROUND (then + min) or (max - min + 1) with INT (then + min).

28.5 Practical Examples

Five short examples that show ROUND and RANDOM in action.

Example 1 — Round a student's average to 2 decimal places

DECLARE Average : REAL
Average <- 87.4567
OUTPUT "Average: ", ROUND(Average, 2)   // Outputs: Average: 87.46

Example 2 — Generate a random dice roll (1–6)

DECLARE DiceRoll : INTEGER
DiceRoll <- ROUND(RANDOM() * 5, 0) + 1
OUTPUT "You rolled: ", DiceRoll

Example 3 — Generate a random percentage (0–100)

DECLARE Percent : INTEGER
Percent <- ROUND(RANDOM() * 100, 0)
OUTPUT "Score: ", Percent, "%"

Example 4 — Round currency to 2 decimal places for display

DECLARE Total : REAL
Total <- 19.987
OUTPUT "$", ROUND(Total, 2)   // Outputs: $19.99

Example 5 — Simulate a coin toss (random 0 or 1)

DECLARE Coin : INTEGER
Coin <- ROUND(RANDOM(), 0)
IF Coin = 0 THEN
  OUTPUT "Heads"
ELSE
  OUTPUT "Tails"
ENDIF

Exam tip: When a question asks you to "simulate" a dice, coin or lottery, reach for RANDOM() and combine with ROUND() using the range formula.

28.6 Key Points Summary

Recap of everything you need to remember about ROUND and RANDOM.

ROUND(value, decimalPlaces) rounds to the specified number of decimal places.
RANDOM() returns a random REAL between 0 and 1 (inclusive) — no parameters.
Use ROUND + arithmetic to generate random integers.
Range [min, max] via ROUND: ROUND(RANDOM() * (max - min - 1), 0) + min.
Range [min, max] via INT: INT(RANDOM() * (max - min + 1)) + min.
Both functions RETURN a value — assign to a variable or use in expressions.
ROUND returns a REAL value, even when the result is a whole number.
Common uses: rounding currency, dice games, lottery, simulations.
Dice 1–6: ROUND(RANDOM() * 5, 0) + 1.
Coin toss: ROUND(RANDOM(), 0) gives 0 or 1.

Question Bank

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

0/12 answered

Question 1Multiple Choice

What does ROUND(3.14159, 4) return?

Question 2True / False

RANDOM() returns an INTEGER between 0 and 1.

Question 3Multiple Choice

DECLARE X : REAL
X <- 3.456
OUTPUT ROUND(X, 2)

Question 4Multiple Choice

Which expression produces a random INTEGER between 1 and 10 inclusive?

Question 5True / False

ROUND(7.5, 0) returns 8.

Question 6Multiple Choice

A teacher wants a random percentage (0–100) for a quiz simulation. Which expression is correct?

Question 7Multiple Choice

DECLARE LotteryNum : INTEGER
LotteryNum <- ROUND(RANDOM() * 48, 0) + 1
OUTPUT LotteryNum

Question 8True / False

ROUND always returns a REAL value, even when the result is a whole number.

Question 9Multiple Choice

What does ROUND(1234.5678, 1) return?

Question 10True / False

The expression ROUND(RANDOM() * 5, 0) + 1 produces a value in the range 1 to 6.

Question 11Multiple Choice

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

Question 12Multiple Choice

DECLARE Score : REAL
INPUT Score
OUTPUT ROUND(Score, 2)

Answer all 12 questions to enable submission.