Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.
This Random whole numbers with Math.random() and Math.floor() worksheet is part of the ACARA Digital Technologies Years 7–8 — Programming with Control Structures and Functions (JavaScript) journey. Print it as a booklet?
Shade in the A or B circle for each question.
Draw a line connecting each item on the left to its match on the right.
| Left column | Right 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 |
Complete each line of code by filling in the missing parts.
Shade in the letter of the line that contains an error.
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.let roll = Math.floor(Math.random() * 6);// random number from 0 to 9
let n = Math.floor(Math.random() * 10);// dice roll from 1 to 6
let n = Math.floor(Math.random() * 6) + 1;// decimal from 0 up to 1
let r = Math.random();// secret number from 1 to 100
let s = Math.floor(Math.random() * 100) + 1;