[8.1.7] Library Routines

[8.1.7] Library Routines: MOD, DIV, ROUND, RANDOM

What this page covers

This page explains how to understand and use four common library routines you will meet in IGCSE programming tasks: MOD, DIV, ROUND, and RANDOM. These are building blocks for many algorithms, such as timetabling with remainders, formatting numbers to a set number of decimal places, and creating fair simulations or games. You will learn what each routine does, typical exam-ready patterns, and pitfalls to avoid. Where helpful, we compare outcomes using small, concrete examples and realistic scenarios.

Quick reference

Routine What it does Typical result type Typical use
DIV Integer division: returns the whole-number quotient of a division, discarding any fractional part. INTEGER Counting full groups: boxes per shipment, rows of seats, pages filled.
MOD Remainder after integer division. INTEGER Leftover items, cyclical patterns (e.g. weekday arithmetic), parity checks (even/odd).
ROUND Rounds a real number to the nearest whole number or to a stated number of decimal places. INTEGER or REAL Displaying prices neatly, limiting measurement precision, formatting output.
RANDOM Generates a pseudo-random number. Depending on the context, this can be a real in [0,1) or an integer within a specified range. REAL or INTEGER Simulations, games (dice/cards), selecting random samples.

Using DIV and MOD together

DIV and MOD are paired operations that fully describe a division of one integer by another: dividend = (divisor × (dividend DIV divisor)) + (dividend MOD divisor). This is extremely useful when splitting things into fixed-size groups and finding the remainder. Remember that DIV gives the count of full groups, while MOD gives what is left over.

Example: 95 students, bus seats 32.

  • Full buses: 95 DIV 32 = 2
  • Leftover students: 95 MOD 32 = 31

So you can fill 2 buses and still have 31 students needing alternative transport.

Scenario: A lesson lasts 55 minutes. How many whole 10-minute activities fit, and how many minutes remain?

  • Activities: 55 DIV 10 = 5
  • Spare minutes: 55 MOD 10 = 5

Plan five 10-minute tasks and leave 5 minutes for plenary or questions.

Edge case: 24 sweets shared into bags of 6.

  • Bags: 24 DIV 6 = 4
  • Leftover: 24 MOD 6 = 0

When the remainder is 0, the division is exact and there are no leftovers.

Using ROUND accurately

ROUND converts a real to the nearest integer or to a specified number of decimal places. In exam contexts the instruction will usually be clear, such as “round to 2 decimal places”. If the digit after your last kept place is 5 or more, round up; otherwise round down. Show working when needed, and be careful with negative numbers and money formatting.

Examples: ROUND(3.2) = 3, ROUND(3.7) = 4. Think: which integer is closer?

Price: £2.374 → ROUND to 2 d.p. = £2.37; £2.375 → ROUND to 2 d.p. = £2.38.

When displaying money, ensure you always show two digits after the decimal point.

.5 tie: Exams typically expect “round half up”. For negatives, apply the same rule carefully: ROUND(-2.5) → -3 (further from zero) when half-up is specified. Always follow the question's rounding rule if given.

Using RANDOM for fair selection

RANDOM provides unpredictable values suitable for simulations. In many tasks, RANDOM() gives a real number in the interval [0, 1). To obtain integers in a range, either use an integer random routine if available (e.g. RANDOM_INT(low, high)) or scale a [0,1) value to the required bounds. State clearly whether the bounds are inclusive.

Example: x ← RANDOM() returns 0 ≤ x < 1. Useful for probability checks such as “10% chance” by testing RANDOM() < 0.1.

Fair die: Use an integer random in the inclusive range 1..6 (each face equally likely). If only RANDOM() is available, compute 1 + (INT)(RANDOM() * 6) and ensure the 6 is reachable.

Seeding: Some languages allow setting a seed so the same “random” sequence can be reproduced for testing. This is helpful for debugging but should be removed for real gameplay.

Combining routines in real tasks

These routines are often used together. For example, a shop might calculate how many complete packs are needed (DIV), how many items are leftover (MOD), offer a prize to a random customer (RANDOM), and display the total cost rounded to two decimal places (ROUND). Thinking in these small steps makes problems manageable and your solutions easier to explain in exams.

Deep Dive: Rounding rules

When questions involve money or a specified number of decimal places, the expected rule is usually “round half up”. If you are using a language routine that behaves differently by default, either show your working conceptually or use an alternative that matches the question. In pseudocode, ROUND(value, dp) is interpreted as “nearest” with .5 rounding up unless told otherwise.

Deep Dive: Pseudo-randomness and fairness

Computers generate pseudo-random numbers by algorithm. For fair outcomes (e.g. a die), ensure the mapping from the random value to outcomes is even. Avoid off-by-one errors when scaling, and confirm whether the upper bound is included.

Copy-ready reminder snippets

// CAIE-style pseudocode reminders
// Packs and leftovers
packs ← items DIV packSize
leftover ← items MOD packSize

// Rounding
price2dp ← ROUND(price, 2)

// Random checks
x ← RANDOM()           // 0 ≤ x < 1
IF x < 0.25 THEN
    OUTPUT "Prize"
ENDIF

// Die 1..6 (if RANDOM() only)
roll ← 1 + INT(RANDOM() * 6)
# Python reminders (mapping to CAIE ideas)
# DIV ↔ //, MOD ↔ %, ROUND ↔ round(x, dp), RANDOM ↔ random.random()/randint()
import random

packs = items // packSize
leftover = items % packSize
price2dp = round(price, 2)
x = random.random()        # 0 ≤ x < 1
roll = random.randint(1, 6)  # inclusive 1..6

 Key Takeaways

  • DIV gives the whole-number quotient; MOD gives the remainder. Use both to split into groups and leftovers.
  • ROUND formats numbers to the nearest whole or to a set number of decimal places; watch tie (.5) and negative cases.
  • RANDOM supports simulations: understand ranges and inclusivity to avoid bias and off-by-one errors.
  • Combine these routines to solve practical tasks such as timetabling, packing, pricing, and games.
  • Be explicit in exams: state bounds for RANDOM and the number of decimal places for ROUND.