Skip to content

Commit 625c114

Browse files
committed
Add comments explaining how num is calculated as a random integer between minimum and maximum.
1 parent b16d048 commit 625c114

File tree

1 file changed

+71
-4
lines changed

1 file changed

+71
-4
lines changed

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

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,74 @@ const maximum = 100;
33

44
const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
55

6-
// In this exercise, you will need to work out what num represents?
7-
// Try breaking down the expression and using documentation to explain what it means
8-
// It will help to think about the order in which expressions are evaluated
9-
// Try logging the value of num and running the program several times to build an idea of what the program is doing
6+
// 1- In this exercise, you will need to work out what num represents?
7+
8+
// the num gives a random whole number between 1 and 100 like 73, 12, or 100.
9+
10+
// 2- Try breaking down the expression and using documentation to explain what it means
11+
/*
12+
13+
1. Math.random()
14+
15+
Returns a random decimal number between 0 and 1 but never gives 1.0.
16+
17+
Example: 0.24
18+
19+
2. (maximum - minimum + 1)
20+
21+
This gives number of possible values.
22+
23+
Without the +1, we'd only get the difference, not the full count.
24+
25+
for example:
26+
27+
5 - 1 = 4 → but there are actually 5 numbers: 1, 2, 3, 4, 5
28+
29+
So we add +1 to include both ends of the range.
30+
31+
3. Math.random() * (maximum - minimum + 1)
32+
33+
This gives a random decimal number between 0 and 100 (like 24, 65 ...)
34+
35+
Because we want the random decimal scaled to the size of the range of possible values.
36+
37+
For example, if we want a number between 1 and 100 (inclusive), there are 100 possible numbers (1, 2, ..., 100).
38+
39+
Multiplying by 100 means the decimal is scaled up to cover all those possibilities before rounding.
40+
41+
4. Math.floor(...)
42+
43+
This rounds the decimal down to the nearest whole number.
44+
45+
Example: Math.floor(78.43) → 78
46+
47+
5. + minimum
48+
49+
we add the minimum to shift the range correctly, and make sure the random number up to start from minimum.
50+
51+
5-1- for example if we remove the + minimum
52+
53+
5-1-1 Math.random() 0.9999 * 99 + 1 → only goes up to 99.999... → max = 99.999... → floor = 100 (but very unlikely)
54+
55+
now 100 becomes very hard to reach, and in many cases, you never get it.
56+
57+
5-1-2 Math.random() 0.00 * 99 + 1 → only goes up to 0... → max = 0... → floor = 0 (now the minimum is 0, and can appears)
58+
59+
conclusion : when we don’t add + minimum, there is a chance that 1 appears, but it’s not the guaranteed minimum anymore —
60+
61+
and the range starts at 0, not 1.
62+
63+
5-2- when we add +minimum
64+
65+
now we make sure the min and max can appear in the final results and make sure the minimum is 1 not 0.
66+
67+
Minimum appears when random = 0
68+
69+
Maximum appears when random is almost 1 (like 0.9999...).
70+
71+
example : Math.random() * 99 + 1 → up to 0.99 → max = 99 → floor = 99 → +1 = 100 (so more possibilities for 100 to appears)
72+
73+
*/
74+
75+
//It will help to think about the order in which expressions are evaluated
76+
//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)