0

I need help width the Random r = new Random(); code.

I will give you my code and tell you what I want to do.

Random r = new Random();
int i = r.nextInt(3);
System.out.println(i);

//now how do i make it not to take 0 as a random number
//One of them is 0, how do i make it take a random int that is
//bigger than 0 but lower than 3. I need help there.
Salmeh
  • 29
  • 9

1 Answers1

2

Well, think about it. You currently get 0, 1, or 2. So how could you get just 1 or 2? You pass 2 into nextInt so you get either 0 or 1, and add 1 to the result:

int i = r.nextInt(2) + 1;
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875