Home > DeveloperSection > Forums > How can I generate random number in specific range in Android?
john rob

Total Post:108

Points:756
Posted on    May-07-2013 12:59 AM

 Android Android 
Ratings:


 1 Reply(s)
 5701  View(s)
Rate this:
Hello everyone,

I want to generate random no. in a specific range for use in my android application. (Ex. Range Between 65 to 80)

I try as per below code, but it is not very use full. It also returns the value greater then max. value(greater then 80).

Random r=new Random();
int i1=(r.nextInt(80) +65);
Anyone have idea How I can generate random number in between range.

Thanks in advance! 


AVADHESH PATEL

Total Post:604

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

Hi John!

Replace your line of code which have following below

Random r = new Random();
int i1=r.nextInt(80-65) + 65;

This gives a random integer between 65 (inclusive) and 80 (exclusive), one of 65,66,...,78,79.

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

Follow MindStick