Random和SecureRandom详解
来源:互联网 发布:交友软件哪个好 编辑:程序博客网 时间:2024/05/22 01:56
[H B BC] Random object created and used only once [DMI_RANDOM_USED_ONLY_ONCE]
This code creates a java.util.Random object, uses it to generate one random number, and then discards the Random object. This produces mediocre quality random numbers and is inefficient. If possible, rewrite the code so that the Random object is created once and saved, and each time a new random number is required invoke a method on the existing Random object to obtain it.
If it is important that the generated Random numbers not be guessable, you must not create a new Random for each random number; the values are too easily guessable. You should strongly consider using a java.security.SecureRandom instead (and avoid allocating a new SecureRandom for each random number needed).
先看看下面这段FindBugs认为有问题的代码:
return new Random(seed).nextInt();
}
看BUG解释,FindBugs建议是:
1.new一个Random的对象,保存之,然后每次都使用这个对象去获取随机数,而不要每次new一个Random对象去获取。
2.FindBugs强烈推荐使用java.security.SecureRandom代替Random。
根据以上要求,我修改了我的代码:
private SecureRandom ran;
Test(int seed){
ran = new SecureRandom();
}
public int getRandom(int seed) {
return ran.nextInt();
}
}
至于为什么FindBugs要这样建议,它这里没有说,我也不太确定,但我的这样猜测的:
1.现在所谓的随机函数Random都是伪随机,不是真正意义上的随机,他们都是使用一个初始值(比如:当前时间),然后把初始值放入随机算法生成随机数。
2.如果每次都new Random()可能会影响随机性,这个可以看看下面代码:
System.out.println(new Random(new Date().getTime()).nextInt());
System.out.println(new Date().getTime());
System.out.println(new Random(new Date().getTime()).nextInt());
System.out.println(new Random(new Date().getTime()).nextInt());
System.out.println(new Random(new Date().getTime()).nextInt());
System.out.println(new Random(new Date().getTime()).nextInt());
System.out.println(new Random(new Date().getTime()).nextInt());
System.out.println(new Random(new Date().getTime()).nextInt());
System.out.println(new Random(new Date().getTime()).nextInt());
System.out.println(new Random(new Date().getTime()).nextInt());
System.out.println(new Random(new Date().getTime()).nextInt());
输出的结果是:
779046670
1235021925734
779046670
779046670
779046670
779046670
779046670
779046670
779046670
779046670
779046670
3.每次都生成对象肯定比每次都使用同一个对象的效率要低。
4.SecureRandom可能是对Random在随机算法上做了改进,使之更安全。(还没看源代码,这个也仅仅是我的猜测而已)
- Random和SecureRandom详解
- Random,ThreadLocalRandom,SecureRandom
- Java中的随机数生成器:Random,ThreadLocalRandom,SecureRandom
- Java中的随机数生成器:Random,ThreadLocalRandom,SecureRandom
- Java中的随机数生成器:Random,ThreadLocalRandom,SecureRandom .
- Java中的随机数生成器:Random,ThreadLocalRandom,SecureRandom
- Java中生成随机数Random、ThreadLocalRandom、SecureRandom、Math.random()
- SecureRandom
- SecureRandom
- [转]Java中的随机数生成器:Random,ThreadLocalRandom,SecureRandom
- java中Random详解以及和Math.random()
- Python学习笔记:详解random模块和time模块
- python random和numpy random
- python,random.random()和random.Random()的区别联系
- SecureRandom 类!!
- SecureRandom 类!!
- SecureRandom简介
- random函数详解
- Web MVC简介
- atmega328p烧写Arduino bootloader
- Ubuntu14.04 Java6 jdk安装与环境配置
- android实现开机自启动服务
- iOS开发常用的第三方类库
- Random和SecureRandom详解
- Spring @Vlaue注解
- HDU1210 Eddy's 洗牌问题【规律】
- java OA开源办公系统源码下载
- 黑马程序员------Foundation框架------NSDictionary字典
- Android中dip(dp)与px之间单位转换 dip2px dp转px 无context算法(以及获取获取屏幕宽度和高度)
- weblogic12.1.3的安装及更新
- 安卓基础(三)
- Controller接口控制器详解(1)