"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. 跳板攻击之:reGeorg 代理转发

    跳板攻击之:reGeorg 代理转发 郑重声明: 本笔记编写目的只用于安全知识提升,并与更多人共享安全知识,切勿使用笔记中的技术进行违法活动,利用笔记中的技术造成的后果与作者本人无关.倡导维护网络安全 ...

  2. 【C#异步】异步多线程的本质,上下文流转和同步

    引言 net同僚对于async和await的话题真的是经久不衰,这段时间又看到了关于这方面的讨论,最终也没有得出什么结论,其实要弄懂这个东西,并没有那么复杂,简单的从本质上来讲,就是一句话,async ...

  3. js控制关闭layui的switch开关

    <input class="switch" type="checkbox" lay-skin="switch" lay-filter= ...

  4. jquery获得标签的值或元素的内容

    例如: .html() 获取a标签中的i元素 console.error($("a[name=" + index + "]").html()); 设置a标签里的 ...

  5. Object.assgin基本知识与相关深浅拷贝

     一.关于Object.assgin()基本知识 概念 Object.assign() 方法用于将所有可枚举属性的值从一个或多个源对象复制到目标对象.它将返回目标对象. 理解 对象的属性分为可枚举和不 ...

  6. Python elasticsearch 使用心得

    一.配置 python == 3.6/3.8 # 更高版本的elasticsearch会出现doc_type被统一成_doc导致旧版语句报错的情况 pip install elasticsearch= ...

  7. javaWeb学习一

    web开发(web就是网页): 静态web html.css 提供给所有人看到数据不会改变 动态web 提供给所有人看到数据会改变,不同用户.不同时间和地点都会不同 技术栈:Servlet/JSP,A ...

  8. 一文总结Vue

    一.创建项目 1.安装Node.js 查看node版本 node-v 查看npm版本 npm-v 2.安装vue-cli脚手架 安装 npm install -g @vue/cli 创建项目 vue ...

  9. 如何将多个TXT合并成一个TXT,文件名称提取

    方法1:1.将所有需要合并的TXT整理到一个文件夹中,切记,TXT合并最好每个TXT内容头或尾留一行间距,因为合并是直接合并,不会保留间距. 2.使用Windows命令cmd,切换到文件所在文件夹 3 ...

  10. leetcode 30. 串联所有单词的子串 【时间击败 90.28%】 【内存击败 97.44%】

    这道题让我从早做到晚-3--- 设len=words[0].length(). 一开始我按照words的顺序扩大区间,发现这样就依赖words的顺序.之后改成遍历s的所有长度为len*words.le ...