Advertise with us

Get random integer in range (x, y]?

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! 
Last updated:5/7/2013 7:44:06 AM

1 Answers

AVADHESH PATEL
AVADHESH PATEL

Hi Manoj!

Try as following 

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

Last updated:5/7/2013 7:44:06 AM

Answer