Skip to content

Commit c4aa0c2

Browse files
committed
Added step by step breakdown of random number generation
1 parent a014507 commit c4aa0c2

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
11
const minimum = 1;
22
const maximum = 100;
33

4-
const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
4+
const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; // Generates a random whole number between minimum and maximum
55

66
// In this exercise, you will need to work out what num represents?
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+
// Generate a random decimal number between 0 and 1
12+
const decimal = Math.random();
13+
// Calculate the range size
14+
const range = maximum - minimum + 1;
15+
// Scale the decimal to the range
16+
const scaled = decimal * range;
17+
// Round down to get a whole number
18+
const floored = Math.floor(scaled);
19+
// Shift the number up to start at the minimum
20+
const result = floored + minimum;

0 commit comments

Comments
 (0)