首页 > 其他 > 详细

RV_ABSOLUTE_VALUE_OF_RANDOM_INT

时间:2020-10-12 17:27:21      阅读:50      评论:0      收藏:0      [点我收藏+]

FindBugs扫描出的问题:

RV: Bad attempt to compute absolute value of signed random integer (RV_ABSOLUTE_VALUE_OF_RANDOM_INT)

This code generates a random signed integer and then computes the absolute value of that random integer. If the number returned by the random number generator is Integer.MIN_VALUE, then the result will be negative as well (since Math.abs(Integer.MIN_VALUE) == Integer.MIN_VALUE). (Same problem arised for long values as well).

问题代码:

Random rd = new Random();
Math.abs(rd.nextInt());

 

解决办法:

使用random.nextInt(Integer.MAX_VALUE)获取从0到MAX_VALUE的数字-1

修改后代码:

Random rd = new Random();
Math.abs(rd.nextInt(Integer.MAX_VALUE));

 

RV_ABSOLUTE_VALUE_OF_RANDOM_INT

原文:https://www.cnblogs.com/penghq/p/13803309.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!