← ACARA Digital Technologies Years 7–8

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.

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 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.

Learning
Duo
Which line gives a random whole number from 0 to 9?
Math.floor(Math.random() * 10)Math.random() * 10
Matches
let num = Math.floor(Math.random * 10);
↓
Math.random is missing its brackets (), so it is not called and no random number is made.
Applying
Meanings
Term
Math.floor(Math.random() * n)
Meaning
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.
Practice
let secret = Math.floor(Math.random() * 10) + 1;
Testing
Gaps
let roll = Math.(Math.random() * 6);
Check Answer
Spots
1let r = Math.random();
2let n = Math.round(r * 6);

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

The 'Spider' badge — Web Weaver, 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 line gives a random whole number from 0 to 9?
A. Math.floor(Math.random() * 10) ✓
B. Math.random() * 10
Math.random() * 10 gives a decimal like 6.43. Math.floor rounds it down to a whole number from 0 to 9.
2. What kind of number does Math.random() give on its own?
A. A whole number from 1 to 10
B. A decimal from 0 up to (but not including) 1 ✓
Math.random() gives a decimal such as 0.372. You need to multiply it and use Math.floor to get whole numbers.
3. What is the biggest number Math.floor(Math.random() * 6) can give?
A. 5 ✓
B. 6
The result goes from 0 to n - 1, so with n = 6 the biggest value is 5.
4. Which line simulates rolling a normal dice (1 to 6)?
A. Math.floor(Math.random() * 6)
B. Math.floor(Math.random() * 6) + 1 ✓
Without + 1 the result is 0 to 5. Adding 1 shifts it to 1 to 6.
5. What does Math.floor() do to 4.9?
A. It rounds it down to 4 ✓
B. It rounds it up to 5
Math.floor always rounds down to the nearest whole number, so 4.9 becomes 4.

Match the Pairs

Math.random() → Gives a random decimal from 0 up to (but not including) 1, such as 0.274
Math.floor(Math.random() * 6) → Gives a random whole number from 0 to 5
To get a random dice roll from 1 to 6, add 1 after rounding down → let roll = Math.floor(Math.random() * 6) + 1;
Pick a secret number from 1 to 100 for a guessing game → let secret = Math.floor(Math.random() * 100) + 1;
let num = Math.floor(Math.random * 10); → Math.random is missing its brackets (), so it is not called and no random number is made.
let num = Math.random() * 10; → Math.floor is missing, so num is a decimal like 7.381 instead of a whole number.

Key Term

Math.floor(Math.random() * n)
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.

Fill in the Blanks

1. let roll = Math.floor(Math.random() * 6);
Hint: Type the Math method that rounds a decimal down to a whole number.
2. // random number from 0 to 9 let n = Math.floor(Math.random() * 10);
Hint: Type the number of different results you want, which is how many numbers there are from 0 to 9.
3. // dice roll from 1 to 6 let n = Math.floor(Math.random() * 6) + 1;
Hint: Type the operator that shifts the result so it starts at 1 instead of 0.
4. // decimal from 0 up to 1 let r = Math.random();
Hint: Type the Math method that gives a random decimal between 0 and 1.
5. // secret number from 1 to 100 let s = Math.floor(Math.random() * 100) + 1;
Hint: Type the arithmetic operator that stretches the random decimal to a bigger range.

Spot the Error

Question 1
A let r = Math.random();
B let n = Math.round(r * 6);
C console.log(n);
Line B — Math.round can round up to 6, so the result is not always from 0 to 5. Use Math.floor, which always rounds down.
Question 2
A let r = Math.random();
B let n = Math.floor(r * 10);
C let roll = n - 1;
D console.log(roll);
Line C — To shift the range so it starts at 1, you add 1 (n + 1). Subtracting 1 would give -1 to 8.
Question 3
A let sides = 8;
B let r = Math.random;
C let n = Math.floor(r * sides);
Line B — Math.random is a function, so it needs brackets to be called. It should be Math.random().
Question 4
A let r = Math.random();
B let num = Math.floor(r * 10);
C num = num + 1;
D console.log(Num);
Line D — JavaScript is case sensitive, so Num is not the same as num. This causes a ReferenceError. It should be console.log(num);
Question 5
A let r = Math.rand();
B let n = Math.floor(r * 6);
C console.log(n + 1);
Line A — There is no Math.rand() function. The correct name is Math.random().

Student with a class code? Join your class →