-3

I wanted to print a random number that starts with 30 and ends with 95. But it still prints 29 and lower. Here's my the code of formula that I used.

ran_num = (95 - 30 + 1) + 30;
moonman
  • 1
  • 1

1 Answers1

0

You can have a random number between 30 and 95 by using a ThreadLocalRandom for example.

A code that prints 10 random numbers in that range could look like this:

for (int i = 0; i < 10; i++) {
    System.out.println(ThreadLocalRandom.current().nextInt(30, 96)); // upper border exclusive
}

If you want to store a random in a variable, write something like

int randomNumber = ThreadLocalRandom.current().nextInt(30, 96);
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
deHaar
  • 17,687
  • 10
  • 38
  • 51