Skip to content

Commit b117813

Browse files
committed
Enhance documentation in 4-random.js to clarify the random number generation process
1 parent 6fd351c commit b117813

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,21 @@ const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
77
// Try breaking down the expression and using documentation to explain what it means
88
// It will help to think about the order in which expressions are evaluated
99
// Try logging the value of num and running the program several times to build an idea of what the program is doing
10+
11+
num //represents a random integer between the minimum value (1) and the maximum value (100).Math
12+
13+
(maximum - minimum + 1) //represents the range of possible values, which is 100 - 1 + 1 = 100.
14+
// This means that there are 100 possible values that num can take on.
15+
16+
Math.random() //generates a random decimal number between 0 and 1. This means that the value of Math.random()
17+
// can be any number between 0 and 1, but it will never be exactly 1.
18+
19+
Math.floor(Math.random() * (maximum - minimum + 1)) //multiplies the random decimal number generated by Math.random()
20+
// by the range of possible values (100), and then rounds down to the nearest integer using Math.floor().
21+
// This means that the result will be a random integer between 0 and 99.
22+
23+
Math.floor(Math.random() * (maximum - minimum + 1)) + minimum //adds the minimum value (1) to the result of the previous
24+
// expression. This means that the final result will be a random integer between 1 and 100, inclusive.
25+
26+
27+

0 commit comments

Comments
 (0)