What does Math.random() return?
A
An integer between 0 and 10 B
A decimal between 0 (inclusive) and 1 (exclusive) C
A random string D
A negative number
Analysis & Theory
Math.random() returns a floating-point number from 0 (inclusive) up to but not including 1.
How would you generate a random integer from 0 to 9?
A
Math.random(10) B
Math.floor(Math.random() * 10) C
Math.round(Math.random() + 10) D
Math.ceil(Math.random()) * 10
Analysis & Theory
Math.floor(Math.random() * 10) gives an integer between 0 and 9.
Which method is used to round down a random number to the nearest integer?
A
Math.round() B
Math.floor() C
Math.ceil() D
parseInt()
Analysis & Theory
Math.floor() always rounds down to the nearest whole number.
How do you generate a random number between 1 and 10 (inclusive)?
A
Math.random() * 10 B
Math.floor(Math.random() * 10 + 1) C
Math.ceil(Math.random() * 10) D
Math.floor(Math.random()) + 10
Analysis & Theory
Math.floor(Math.random() * 10 + 1) gives integers from 1 to 10.
What does Math.ceil(Math.random() * 6) return?
A
0 to 5 B
1 to 6 C
1 to 5 D
0 to 6
Analysis & Theory
This is commonly used for simulating a dice roll (1 to 6).
Which of the following returns true if a number is between 0 and 1?
A
Math.random() > 0 && Math.random() < 1 B
Math.random() < 0 C
Math.random() > 1 D
Math.random() == 1
Analysis & Theory
All values from Math.random() are between 0 (inclusive) and 1 (exclusive).
What is the range of Math.random() * 100?
A
0 to 100 B
0 to 99 C
0 (inclusive) to 100 (exclusive) D
1 to 100
Analysis & Theory
Math.random() * 100 gives a floating-point number from 0 up to but not including 100.
How do you generate a random even number between 2 and 10 (inclusive)?
A
Math.floor(Math.random() * 4 + 1) * 2 B
Math.random() * 10 / 2 C
Math.random() + 2 D
Math.ceil(Math.random() * 4) + 2
Analysis & Theory
Math.floor(Math.random() * 4 + 1) * 2 generates 2, 4, 6, 8, 10.
What will Math.floor(Math.random() * 1) return?
A
0 or 1 B
1 C
0 D
Random number
Analysis & Theory
Math.random() * 1 is always less than 1, so Math.floor() gives 0.
How can you generate a random letter from A–Z?
A
String.fromCharCode(Math.random() * 26) B
String.fromCharCode(Math.floor(Math.random() * 26) + 65) C
Math.random().charAt(0) D
randomString(26)
Analysis & Theory
ASCII for 'A' is 65, so adding 0–25 gives characters A–Z.