How can I generate random number in specific range in Android? [duplicate] How can I generate random number in specific range in Android? [duplicate] android android

How can I generate random number in specific range in Android? [duplicate]


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.


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

Note that nextInt(int max) returns an int between 0 inclusive and max exclusive. Hence the +1.