Random whole numbers with Math.random() and Math.floor()

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Random whole numbers with Math.random() and Math.floor()

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line gives a random whole number from 0 to 9?
A Math.floor(Math.random() * 10)
B Math.random() * 10
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
3. What is the biggest number Math.floor(Math.random() * 6) can give?
A 5
B 6
4. Which line simulates rolling a normal dice (1 to 6)?
A Math.floor(Math.random() * 6)
B Math.floor(Math.random() * 6) + 1
5. What does Math.floor() do to 4.9?
A It rounds it down to 4
B It rounds it up to 5

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

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

Definitions

Math.floor(Math.random() * n)

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
let roll = Math._____(Math.random() * 6);
2.
// random number from 0 to 9 let n = Math.floor(Math.random() * __);
3.
// dice roll from 1 to 6 let n = Math.floor(Math.random() * 6) _ 1;
4.
// decimal from 0 up to 1 let r = Math.______();
5.
// secret number from 1 to 100 let s = Math.floor(Math.random() _ 100) + 1;

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A let r = Math.random();
B let n = Math.round(r * 6);
C console.log(n);
Question 2
A let r = Math.random();
B let n = Math.floor(r * 10);
C let roll = n - 1;
D console.log(roll);
Question 3
A let sides = 8;
B let r = Math.random;
C let n = Math.floor(r * sides);
Question 4
A let r = Math.random();
B let num = Math.floor(r * 10);
C num = num + 1;
D console.log(Num);
Question 5
A let r = Math.rand();
B let n = Math.floor(r * 6);
C console.log(n + 1);

Answer Key

Random whole numbers with Math.random() and Math.floor()
Section A: Quick Questions
Q1
A — Math.floor(Math.random() * 10)
Q2
B — A decimal from 0 up to (but not including) 1
Q3
A — 5
Q4
B — Math.floor(Math.random() * 6) + 1
Q5
A — It rounds it down to 4
Section B: Match the Pairs
Q1
Math.random() → Gives a random decimal from 0 up to (but not including) 1, such as 0.274
Q2
Math.floor(Math.random() * 6) → Gives a random whole number from 0 to 5
Q3
To get a random dice roll from 1 to 6, add 1 after rounding down → let roll = Math.floor(Math.random() * 6) + 1;
Q4
Pick a secret number from 1 to 100 for a guessing game → let secret = Math.floor(Math.random() * 100) + 1;
Q5
let num = Math.floor(Math.random * 10); → Math.random is missing its brackets (), so it is not called and no random number is made.
Q6
let num = Math.random() * 10; → Math.floor is missing, so num is a decimal like 7.381 instead of a whole number.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
let roll = Math.floor(Math.random() * 6);
Q2
// random number from 0 to 9 let n = Math.floor(Math.random() * 10);
Q3
// dice roll from 1 to 6 let n = Math.floor(Math.random() * 6) + 1;
Q4
// decimal from 0 up to 1 let r = Math.random();
Q5
// secret number from 1 to 100 let s = Math.floor(Math.random() * 100) + 1;
Section E: Spot the Error
Q1
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.
Q2
C — To shift the range so it starts at 1, you add 1 (n + 1). Subtracting 1 would give -1 to 8.
Q3
B — Math.random is a function, so it needs brackets to be called. It should be Math.random().
Q4
D — JavaScript is case sensitive, so Num is not the same as num. This causes a ReferenceError. It should be console.log(num);
Q5
A — There is no Math.rand() function. The correct name is Math.random().