Home > DeveloperSection > Forums > Get random integer in range (x, y]?
Manoj Bhatt
Manoj Bhatt

Total Post:153

Points:1079
Posted on    May-07-2013 1:16 AM

 Java Java 
Ratings:


 1 Reply(s)
 1041  View(s)
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! 


AVADHESH PATEL

Total Post:604

Points:4228
Posted on    May-07-2013 7:44 AM

Hi Manoj!

Try as following 

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

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

Don't want to miss updates? Please click the below button!

Follow MindStick