- `Random rand = new Random();`
- `p += rand.nextInt(2); //取0-2(左闭右开不包括2)的整数`
public int nextInt(int n)
/**
* Returns a pseudo-random uniformly distributed {@code int}
* in the half-open range [0, n).
*/
public int nextInt(int n) {
if (n <= 0) {
throw new IllegalArgumentException("n <= 0: " + n);
}
if ((n & -n) == n) {
return (int) ((n * (long) next(31)) >> 31);
}
int bits, val;
do {
bits = next(31);
val = bits % n;
} while (bits - val + (n - 1) < 0);
return val;
}
rand.nextInt(b-a+1)+a
rand.nextInt(b-a)+a
int randNum = rand.nextInt(300-10+1) + 10;
Random random = new Random();
Integer res = random.nextInt(n);
Integer res = (int)(Math.random() * n);
【JAVA】产生随机数:rand.nextInt(int n )
原文:https://www.cnblogs.com/anliux/p/12327584.html