+ 1

What's the purpose of "+1" in (max - min +1)? In a range of (3, 7) how does the "+1" work I'm confused

function getRndInteger(min, max) { return Math.floor(Math.random() * (max - min + 1) ) + min; }

12th Aug 2022, 11:35 PM
Umar Akorede
Umar Akorede - avatar
5 Antworten
+ 2
Math.random() returns a precision double value between 0 to 1. Ex: 0.123456789 Multiplying a number N returns 0 to N (N not inclusive) Math.floor returns a returns integer equalent value. Adding Min (ex:1) means value returned is between (Min, N+min) Math.random() => 0 to 1 Math.random() * N => 0 to N (Math.random() *N) +1 => 1 to N+1 Math.floor(Math.random() * (max - min + 1) ) + min; => returns between min to max, Integer values. Hope it helps..
13th Aug 2022, 8:53 AM
Jayakrishna 🇮🇳
+ 1
JavaScript Random https://www.w3schools.com/js/js_random.asp // Returns a random integer from 0 to 9: Math.floor(Math.random() * 10); // Returns a random integer from 1 to 10: Math.floor(Math.random() * 10) + 1;
12th Aug 2022, 11:52 PM
SoloProg
SoloProg - avatar
+ 1
So it's basically means inclusive of the max number?
13th Aug 2022, 12:41 AM
Umar Akorede
Umar Akorede - avatar
0
Thanks pal
14th Aug 2022, 12:19 PM
Umar Akorede
Umar Akorede - avatar