-1

Im wondering how to generate a random number in Java inclusive between 5-15

I have

Random random = new Random();
int count = random.nextInt(10) + 5;
Enrique
  • 51
  • 6

1 Answers1

2

random.nextInt(N) will give you a random integer ranging from 0 to N-1. If you want it from 5 to 15, the range on that is 15-5+1 which is 11.

You would need to use random.nextInt(11) to set the range from [0,11) which is [0,10] and then add a +5 to shift it over to [5,15].

int count = random.nextInt(11) + 5;
faris
  • 692
  • 4
  • 18