Random whole numbers with Math.random() and Math.floor()
Gives a random whole number from 0 up to n - 1. Math.random() gives a decimal from 0 up to 1, times n stretches it, and Math.floor rounds down. Add 1 to start at 1.
Understanding Random whole numbers with Math.random() and Math.floor() in ACARA Digital Technologies Years 7–8
What it does: The part inside gives 0 to 5. Adding 1 moves the range to 1 to 6, just like a real dice. Multiplying by 100 and rounding down gives 0 to 99. Adding 1 makes the secret number between 1 and 100. Math.random() gives a decimal such as 0.372. You need to multiply it and use Math.floor to get whole numbers. The result goes from 0 to n - 1, so with n = 6 the biggest value is 5. Without + 1 the result is 0 to 5. Adding 1 shifts it to 1 to 6. JavaScript is case-sensitive. Math must start with a capital M, or you get a ReferenceError.
Common mistakes: Math.round can round up to 6, so the result is not always from 0 to 5. Use Math.floor, which always rounds down. To shift the range so it starts at 1, you add 1 (n + 1). Subtracting 1 would give -1 to 8.
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 635–666) and word search (Modules 635–695).
Take this worksheet further — free with Cloodit
Each concept is reinforced from every angle — learned, then applied, then tested.
Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.
Over 100 of them, across seven natural biomes.
Turns their work into revision material ready for Flash Cards and Exam study.
Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.
Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.
Quick Questions
Match the Pairs
Key Term
Math.floor(Math.random() * n)Fill in the Blanks
Spot the Error
Student with a class code? Join your class →