This commit is contained in:
gauthiier 2023-01-15 14:15:14 +01:00
parent 8e9af8b04c
commit 0c53817f75

View File

@ -140,7 +140,7 @@ Rudimentary indeed, but there are many things we can build with this simple func
Since we access elements of an array with indices, we can generate a random index between 0 and the length of an array so to select a random element in it. Since we access elements of an array with indices, we can generate a random index between 0 and the length of an array so to select a random element in it.
The first problem we have to solve is that random gives a "real" number (ex: 4.993319470244624, 1.9486631456631776, 7.1841821754535813, etc.) while the indices of an array are "integer" numbers (5, 2, 7, etc.). We thus need to round the "real" numbers produced by random (4.993319470244624 -> 5, 1.9486631456631776 -> 2, 7.1841821754535813 -> 7). We do this with the "Math.floor: function: The first problem we have to solve is that random gives a "real" number (ex: 4.993319470244624, 1.9486631456631776, 7.1841821754535813, etc.) while the indices of an array are "integer" numbers (5, 2, 7, etc.). We thus need to round the "real" numbers produced by random (4.993319470244624 -> 5, 1.9486631456631776 -> 2, 7.1841821754535813 -> 7). We do this with the ```Math.floor``` function:
```javascript ```javascript
let random_number = Math.floor(random(10)); // assign a random integer number between 0 and 10 to variable random_number let random_number = Math.floor(random(10)); // assign a random integer number between 0 and 10 to variable random_number