随机数Random和SecureRandom
"Random" objects should be reused
- Bug
- Critical
- Main sources
- owasp-a6
- Available SinceNov 16, 2021
- SonarAnalyzer (Java)
- Constant/issue: 5min
Creating a new Random object each time a random value is needed is inefficient and may produce numbers which are not random depending on the JDK. For better efficiency and randomness, create a single Random, then store, and reuse it.
The Random() constructor tries to set the seed with a distinct value every time. However there is no guarantee that the seed will be random or even uniformly distributed. Some JDK will use the current time as seed, which makes the generated numbers not random at all.
This rule finds cases where a new Random is created each time a method is invoked and assigned to a local random variable.
Noncompliant Code Example
public void doSomethingCommon() {
Random rand = new Random(); // Noncompliant; new instance created with each invocation
int rValue = rand.nextInt();
//...
Compliant Solution
private Random rand = SecureRandom.getInstanceStrong(); // SecureRandom is preferred to Random
public void doSomethingCommon() {
int rValue = this.rand.nextInt();
//...
Exceptions
A class which uses a Random in its constructor or in a static main function and nowhere else will be ignored by this rule.
要修改的代码:
Random ran = new Random();
int num = ran.nextInt(99999);
修改为:
private Random rand; // SecureRandom is preferred to Random
{
try {
rand = SecureRandom.getInstanceStrong();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
int num = this.rand.nextInt(99999);
以上是sonarqube的修改建议,但是发布后遇到了阻塞问题,参考文章如下
于是,我改成了
private static final Random rand = new Random();
不再阻塞
随机数Random和SecureRandom的更多相关文章
- 真伪随机数 ——Random和SecureRandom
Random Random用来创建伪随机数.所谓伪随机数,是指只要给定一个初始的种子,产生的随机数序列是完全一样的. 要生成一个随机数,可以使用nextInt().nextLong().nextFlo ...
- Java如何生成随机数 - Random、ThreadLocalRandom、SecureRandom
Java7 的Random伪随机数和线程安全的ThreadLocalRandom 一.Random伪随机数: Random 类专门用于生成一个伪随机数,它有两个构造器: 一个构造器使用默认的种子(以当 ...
- (五)boost库之随机数random
(五)boost库之随机数random boost库为我们提供了许多的日常随机数生成器: 1.uniform_smallint:在小整数域内的均匀分布 2.uniform_int:在整数域上的均匀分布 ...
- Python 随机数 random
1. Python seed() 函数 seed() 方法改变随机数生成器的种子,可以在调用其他随机模块函数之前调用此函数. seed( )是不能直接访问的,需要导入 random 模块,然后 ...
- [Swift] 随机数 | Random numbers
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- JS对象随机数 random() 方法可返回介于 0 ~ 1(大于或等于 0 但小于 1 )之间的一个随机数。 注意:返回一个大于或等于 0但小于1的符号为正的数值
随机数 random() random() 方法可返回介于 0 ~ 1(大于或等于 0 但小于 1 )之间的一个随机数. 语法: Math.random(); 注意:返回一个大于或等于 0 但小于 1 ...
- 常用内置模块之collections模块、时间模块、随机数random模块
今日内容回顾 目录 今日内容回顾 包的具体使用 编程思想的转变 软件开发目录规范 常用内置模块之collections模块 常用内置模块之时间模块 常用内置模块之随机数random模块 报的具体使用 ...
- 随机数(random)
需求 Random rd=new Random(); 需要十以内的随机数 (0---10) System.out.println((int)((rd.nextDouble()*100)/10)); ...
- shell脚本获取随机数random
用C提供的取随机数的方法srand和rand, 前者是给后者设置随机数种子seed. ; srand(seed); // time(NULL) 通常使用时间做种子 rnd_num = rand(); ...
- Python之数学(math)和随机数(random)
math包包含了最基本的数学运算函数,如果想要更加高级的数学功能,可以使用标准库外的numpy和scipy库,他们不但支持数组和矩阵运算, 还有丰富的数学和物理方程可供使用 random包可以用来生成 ...
随机推荐
- vue2和vue3区别
1. vue2和vue3双向数据绑定原理发生了改变 vue2的双向数据绑定是利用了es5 的一个API Object.definepropert() 对数据进行劫持 结合发布订阅模式来实现的.vue3 ...
- html(Angular) 调用本地安装exe程序
1.写注册表 新建 .reg文件 Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\creoparametric] "URL P ...
- cximage菜单(Mirror)
// ID_CXIMAGE_MIRROR 文件:CxImage\demo\demoDoc.cpp 菜单项:cximage->Mirror ON_COMMAND(ID_CXIMAGE_MIRROR ...
- 银河麒麟V10在线安装Postgresql步骤
参考资料https://blog.csdn.net/u010430471/article/details/81663248 https://blog.csdn.net/qq_41619524/arti ...
- Day 13 13.1 refer反爬
Referer 一.referer是什么: 图片防盗链的技术应该还有其他的,目前了解到的是浏览器的referer,其实这是错误的拼写,正确是应该是referrer.不过现在可以看到Chrome的开发者 ...
- LG8768 题解
题意 传送门 求长度为 \(n\) 的序列 \(a\) 的个数对 \(998244353\) 取模的结果,其中 \(a\) 满足: \(a_1=w\) \(a_{i-1}+L\le a_i\le a_ ...
- SOJ1728 题解
题意 有一个长度为 \(n\) 的数列 \(a_0,a_1,\dots,a_{n-1}\) 以及一个长度为 \(m\) 的操作序列 \((b_0,c_0),(b_1,c_1)\dots(b_{m-1} ...
- c# 在自定义类中控制form窗体中的控件 赋值或修改属性
c# 在自定义类中控制form窗体中的控件 赋值或修改属性 首先在 自定义类 的外面 声明一个 委托模块 //声明一个委托模块 用来改变form1 窗体中的控件值 public delegate v ...
- 在npm中定义变量
Node_Dev=Dev 在js文件里可以通过prosess.env获取该变量
- Java面向对象之什么是多态?
多态 动态编译:类型:可扩展性 即同一方法可以根据发送对象的不同而采用多种不同的行为方式. 一个对象的实际类型是确定的,但可以指向对象的引用的类型有很多. 多态存在的条件: 1.有继承关系,类型转换异 ...