+ 2

How to print a random number between two numbers?

The random number should be between m and n,where m=5 and n = 10

8th Oct 2017, 11:22 AM
Napster
Napster - avatar
2 Answers
+ 7
int x = rand.nextInt(6) + 5; rand.nextInt(6) means 0-5 Add 5 to it means 5-10
8th Oct 2017, 11:36 AM
Meharban Singh
Meharban Singh - avatar
+ 4
randomNum = minimum + (int)(Math.random() * maximum); or Random rn = new Random(); int n = maximum - minimum + 1; int i = rn.nextInt() % n; randomNum = minimum + i;
8th Oct 2017, 11:42 AM
MsJ
MsJ - avatar