"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的修改建议,但是发布后遇到了阻塞问题,参考文章如下

https://blog.csdn.net/xingyuncaojun/article/details/109390864?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1.pc_relevant_default&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1.pc_relevant_default&utm_relevant_index=1

于是,我改成了

private static final Random rand = new Random(); 

不再阻塞

随机数Random和SecureRandom的更多相关文章

  1. 真伪随机数 ——Random和SecureRandom

    Random Random用来创建伪随机数.所谓伪随机数,是指只要给定一个初始的种子,产生的随机数序列是完全一样的. 要生成一个随机数,可以使用nextInt().nextLong().nextFlo ...

  2. Java如何生成随机数 - Random、ThreadLocalRandom、SecureRandom

    Java7 的Random伪随机数和线程安全的ThreadLocalRandom 一.Random伪随机数: Random 类专门用于生成一个伪随机数,它有两个构造器: 一个构造器使用默认的种子(以当 ...

  3. (五)boost库之随机数random

    (五)boost库之随机数random boost库为我们提供了许多的日常随机数生成器: 1.uniform_smallint:在小整数域内的均匀分布 2.uniform_int:在整数域上的均匀分布 ...

  4. Python 随机数 random

    1. Python seed() 函数     seed() 方法改变随机数生成器的种子,可以在调用其他随机模块函数之前调用此函数. seed( )是不能直接访问的,需要导入 random 模块,然后 ...

  5. [Swift] 随机数 | Random numbers

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  6. JS对象随机数 random() 方法可返回介于 0 ~ 1(大于或等于 0 但小于 1 )之间的一个随机数。 注意:返回一个大于或等于 0但小于1的符号为正的数值

    随机数 random() random() 方法可返回介于 0 ~ 1(大于或等于 0 但小于 1 )之间的一个随机数. 语法: Math.random(); 注意:返回一个大于或等于 0 但小于 1 ...

  7. 常用内置模块之collections模块、时间模块、随机数random模块

    今日内容回顾 目录 今日内容回顾 包的具体使用 编程思想的转变 软件开发目录规范 常用内置模块之collections模块 常用内置模块之时间模块 常用内置模块之随机数random模块 报的具体使用 ...

  8. 随机数(random)

    需求 Random rd=new Random(); 需要十以内的随机数  (0---10) System.out.println((int)((rd.nextDouble()*100)/10)); ...

  9. shell脚本获取随机数random

    用C提供的取随机数的方法srand和rand, 前者是给后者设置随机数种子seed. ; srand(seed); // time(NULL) 通常使用时间做种子 rnd_num = rand(); ...

  10. Python之数学(math)和随机数(random)

    math包包含了最基本的数学运算函数,如果想要更加高级的数学功能,可以使用标准库外的numpy和scipy库,他们不但支持数组和矩阵运算, 还有丰富的数学和物理方程可供使用 random包可以用来生成 ...

随机推荐

  1. 分布式任务调度平台XXL-JOB安装

    安装xxl-job-admin 1.拉取镜像 #拉取镜像 docker pull xuxueli/xxl-job-admin:2.3.0 #新建挂载目录 mkdir /usr/local/xxl-jo ...

  2. Vue3 ref 模板引用获取不到节点

    ref模板引用必须要在组件实例挂载完成之后才可以访问.如果你是在组合式 API 里面写的组件,那么 setup 函数比任何周期函数都早,所以不可能在该函数中执行时获取得到ref--组件实例. 官网关于 ...

  3. Java中的static关键字作用及其应用

    java中的static关键字主要用于内存管理.我们可以应用java static关键字在变量,方法,块和嵌套类中. static关键字属于类,而不是类的实例. static可以是: 1.变量     ...

  4. LeetCode-41 缺失的第一个正整数

    来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/first-missing-positive 题目描述 给你一个未排序的整数数组 nums ,请你 ...

  5. typescript - 学习档案

    由于内容繁多,使用掘金来记录此笔记,方便索引跟随!未完待续~~~ 地址如下: https://juejin.cn/post/6899350420541014030/#heading-20

  6. 06 RDD编程

    总共有多少学生?map(), distinct(), count() 开设了多少门课程? 每个学生选修了多少门课?map(), countByKey() 每门课程有多少个学生选?map(), coun ...

  7. CSS:盒子_每个元素都有两个盒子(《CSS世界》笔记-块级元素)

    CSS:盒子_每个元素都有两个盒子(<CSS世界笔记>-块级元素) 1)CSS世界只有"块级盒子(block-level box)"和"内联盒子(inline ...

  8. [后端-Flask总结]-flask学习总结

    1.flask开发基础与入门: 1.1web开发基础 1.1.1 前端框架: bootstrap, j-query, angular, react 1.2 flask 路由 from flask im ...

  9. 当越来越多的企业放弃使用FTP,该用什么更好的方式替代?

    FTP作为第一个完整的文件传输协议,在互联网技术发展史上具有浓墨重彩的意义,它解决了文件传输协议有无的问题,在全世界范围内被广泛使用.但如今,随着网络技术的发展,企业生产类型和生产资料的丰富化,文件传 ...

  10. c++练习271题:水仙花数

    *271题 原题传送门:http://oj.tfls.net/p/271 题解: #include<bits/stdc++.h>using namespace std; int cf(in ...