而用nextInt()也可以呀:
Random random = new Random();
int a = (random.nextInt(100) >>> 1) % 100;
那他们有什么区别,用于什么不同的情况?
...全文
679756打赏收藏
请问Random中的nextInt()和nextInt(int n)的区别是什么?
请问Random中的nextInt()和nextInt(int n)的区别是什么? nextInt(100) 可以产生100以内的随机数 而用nextInt()也可以呀: Random random = new Random(); int a = (random.nextInt(100) >>> 1) % 100; 那他们有什么区别,用于什么不同的情况?
下面的这段摘自java2 sdk的文档
int nextInt()
Returns the next pseudorandom, uniformly distributed int value from this random number generator's sequence.
int nextInt(int n)
Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.