Skip to content

Commit 64003b5

Browse files
committed
Explain random number generation with Math.random and Math.floor
1 parent 52e2cb3 commit 64003b5

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

‎Sprint-2/1-key-exercises/4-random.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,19 @@ const maximum = 100;
44
const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
55

66
// In this exercise, you will need to work out what num represents?
7+
//num represents a randomly generated whole number between 1 and 100.
8+
79
// Try breaking down the expression and using documentation to explain what it means
10+
// Math.random() generates a random decimal number greater than or equal to 0 and less than 1. Multiplying it by 100 gives a number between 0 and 100.
11+
// Math.floor() always rounds down and returns the largest integer less than or equal to a given number in this case between 0 and 99. Finally, adding 1 changes the range to 1–100.
12+
// Math.floor(Math.random() * (maximum - minimum + 1))
13+
// Math.floor(Math.random() * (100 - 1 + 1)
14+
// Math.floor(Math.random() * 100)
15+
// Math.random give any random number between 0 to 1 example 0.225 * 100 = 22.5
16+
// Math.floor rounds that number to a whole number so 22.5 will become 22
17+
// (22) + minimum
18+
// 22 + 1
19+
// 23
20+
821
// It will help to think about the order in which expressions are evaluated
922
// Try logging the value of num and running the program several times to build an idea of what the program is doing

0 commit comments

Comments
 (0)