GET RANDOM INTEGER IN RANGE (X, Y]?

Manoj Bhatt

Total Post:154

Points:1086
Posted by  Manoj Bhatt
Java 
 1388  View(s)
Ratings:
Rate this:
Hi Everyone! 

How do I generate a random integer i, such that i belongs to (0,10]?

I tried to use this:

Random generator = new Random();
int i = generator.nextInt(10);
but it gives me values between [0,10).

But in my case I need them to be (0,10].

Thanks in advance! 
  1. AVADHESH PATEL

    Post:604

    Points:4228
    Re: Get random integer in range (x, y]?

    Hi Manoj!

    Try as following 

    Random generator = new Random(); 
    int i = generator.nextInt(10) + 1;

      Modified On May-07-2013 07:55:21 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!