随机数Random报错 IllegalArgumentException: Bound must be positive
Posted lwx-apollo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了随机数Random报错 IllegalArgumentException: Bound must be positive相关的知识,希望对你有一定的参考价值。
错误代码
private static Random RANDOM = new Random();
private static Integer randomOnce(Integer max, Integer min) {
return RANDOM.nextInt(max) % (max - min + 1) + min;
}
报错信息
java.lang.IllegalArgumentException: bound must be positive
at java.util.Random.nextInt(Random.java:388)
解决方案
private static Random RANDOM = new Random();
private static Integer randomOnce(Integer max, Integer min) {
if (max<=0)
throw new IllegalArgumentException("Random positive must be greater than 0");
return RANDOM.nextInt(max) % (max - min + 1) + min;
}
以上是关于随机数Random报错 IllegalArgumentException: Bound must be positive的主要内容,如果未能解决你的问题,请参考以下文章
随机数Random报错 IllegalArgumentException: Bound must be positive