Skip to content

Instantly share code, notes, and snippets.

@li2
Last active March 20, 2018 07:09
Show Gist options
  • Save li2/1b44885e8e63f5db678f to your computer and use it in GitHub Desktop.
Save li2/1b44885e8e63f5db678f to your computer and use it in GitHub Desktop.
[如何生成指定范围内的随机数?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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment