Math.floor(Math.random() * (max - min + 1)) + min
Can someone explain how this formula works?
Think of it in this way. If you want to generate random numbers between 2 and 5, that means the four numbers (2, 3, 4, and 5) need to be possibly generated. Math.random returns values between 0 (inclusive) up to but not including 1. You must add 1 to (max - min) or you would end up not including the highest number (5). The
+ min gets added, so the lowest number will be min.