HOW CAN I GENERATE RANDOM NUMBER IN SPECIFIC RANGE IN ANDROID?

john rob

Total Post:108

Points:756
Posted by  john rob
 6517  View(s)
Ratings:
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! 
  1. AVADHESH PATEL

    Post:604

    Points:4228
    Re: How can I generate random number in specific range in Android?

    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.

Answer

NEWSLETTER

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