Random.nextInt()生成负数?为什么不是说Random.nextInt()生成的都是0-n的正整数么.在这个程序里我把随机数打印出来了,却是负数.这是为什么?

来源:学生作业帮助网 编辑:作业帮 时间:2024/04/27 20:58:28
Random.nextInt()生成负数?为什么不是说Random.nextInt()生成的都是0-n的正整数么.在这个程序里我把随机数打印出来了,却是负数.这是为什么?

Random.nextInt()生成负数?为什么不是说Random.nextInt()生成的都是0-n的正整数么.在这个程序里我把随机数打印出来了,却是负数.这是为什么?
Random.nextInt()生成负数?为什么
不是说Random.nextInt()生成的都是0-n的正整数么.在这个程序里我把随机数打印出来了,却是负数.这是为什么?

Random.nextInt()生成负数?为什么不是说Random.nextInt()生成的都是0-n的正整数么.在这个程序里我把随机数打印出来了,却是负数.这是为什么?
如果调用.nextInt(),-MaxValue到MaxValue,.nextInt(n)才是0到n.
下面是JAVA API的原段
nextInt()
Returns the next pseudorandom, uniformly distributed int value from this random number generator's sequence.