How can I generate random number in specific range in Android?

Possible Duplicate:
Java: generating random number in a range

I want to generate random number in a specific range. (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);

How can I generate random number in between range?


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

这给出了一个在65(含)和80(不含)之间的随机整数, 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 .

链接地址: http://www.djcxy.com/p/2930.html

上一篇: 访问Flash CS4 IDE中的舞台

下一篇: 如何在Android中的特定范围内生成随机数?