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
.
上一篇: 访问Flash CS4 IDE中的舞台