0

how do i get random number for 10 to 99
is this right
Random generator = new Random();
int num1;
num1 = generator.nextInt(99)+ 10;
System.out.println(+num1); or

or this is right does it go from 0 to 99???

num1 = generator.nextInt(99);
System.out.println(+num1);
which is the code for 10 to 99

2
Contributors
1
Reply
2
Views
10 Years
Discussion Span
Last Post by Ezzaral
0

num1 = generator.nextInt(90)+ 10; will add a random int between 0 and 89 to your base value of 10.
From the API doc:

Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive)

A little bit of experimentation would have quickly yielded the answer for you. If you had placed that code in a throwaway test class main() method, you could see the result pretty quickly. Don't be afraid to test little things like this in isolation to gain a better understanding of the API.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.