li2
2/24/2016 - 8:24 AM

#

[如何生成指定范围内的随机数?How can i generate random number in specific range in android?] http://stackoverflow.com/questions/6029495/how-can-i-generate-random-number-in-specific-range-in-android

#tags: java


/**
 * Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and
 * the specified value (exclusive), drawn from this random number generator's sequence.
 * 返回一个在 [0, n-1] 之间均匀分布的伪随机数.
 * 
 * @return [0, n-1]
 */
int nextInt(int n);


// example
int min = 65;
int max = 80;

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