+ 2

How can i get a random number between two numbers?

22nd Feb 2017, 1:56 AM
Yalid Sebi Fuentes
Yalid Sebi Fuentes - avatar
3 Answers
+ 4
also, wrap everything in Math.floor for it to be an integer number only as Math.random() returns a number in the range [0, 1)
22nd Feb 2017, 9:08 AM
Burey
Burey - avatar
+ 2
Math.random() * (max - min) + min;
22nd Feb 2017, 4:23 AM
Garrett Barlocker
Garrett Barlocker - avatar
+ 1
i can give the java code . let us take the two numbers as 5 and 10 let us store it in n then the syntax will be int n=(int)((Math.random()*(10-5))+5); now explanation (int) for getting an int number (10-5) to get the number of random numbers to be generated so the number generated can be 0 also so we add the lower limit 5 .so that the lowest number generated is the lower limit only. be aware the largest number generated in this case is 9 .you will have to adjust the code accordingly.
22nd Feb 2017, 4:53 PM
chandranu dhabal
chandranu dhabal - avatar