"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. Mac监控键盘输入并执行动作

    背景 电脑的安全是非常重要的,特别是里面的敏感数据,若是被有心之人利用,那后果不堪设想. 所以我们部门定下了一个规矩,谁离开工位要是不锁屏,就可以在部门群发送一个消息:我请大家吃鸡翅. oh,技术出身 ...

  2. webserver 简单demo

    以调用天气服务为例: 1.添加服务引用 2.点窗口做下角(高级->添加web引用->URL输入框输入天气服务地址(http://www.webxml.com.cn/WebServices/ ...

  3. Optimum + ONNX Runtime: 更容易、更快地训练你的 Hugging Face 模型

    介绍 基于语言.视觉和语音的 Transformer 模型越来越大,以支持终端用户复杂的多模态用例.增加模型大小直接影响训练这些模型所需的资源,并随着模型大小的增加而扩展它们.Hugging Face ...

  4. loj2839

    除了 L 神 txdy 我还能说啥呢.(L 神把这题搬模拟赛了...) 即把每个 x 换成 ( 或 ),问是否能通过不多于一次区间反转(( 与 ) 交换)后合法. 考虑怎样的括号串是合法的. 假设左括 ...

  5. 3.2 删除XxxControler中各方法中的response

    3.2.1 EmpController代码 package com.hy.controller; import javax.servlet.http.HttpServletRequest; impor ...

  6. shell脚本程序的撰写

    一.让我们浏览一下整体步骤第一步,你要先用ll命令来列出该目录中都包含那些文件(目的是为了防止文件重名不利于查找等等).第二步 ,使用命令touch来建立一个shell脚本文件 , 后缀名为.sh : ...

  7. iperf3带宽性能检测工具

    iperf3用来测试两个节点之间的带宽传输,无论是几百k,还是几个G,基本可以满足两端的带宽测速. 1.软件环境 iperf3常用 windows环境 官网下载安装包,下载完成后解压,使用时,切换到c ...

  8. C++中链表报错member access within null pointer of type 'ListNode'

    报错原因:指针有指向空节点的可能,所以报错,C++中链表的使用比较严格 解决方法:在给指针确定指向节点之前,先判断此节点是否为空节点

  9. python日志logging配置

    python日志logging配置 为了方便ELK收集日志,将日志打印成json格式 开发过程中,使用json格式不方便排查问题 本文章使用python的logging模块,一步步增加配置,来说明每个 ...

  10. 在k8s中使用harbor仓库

    在k8s中使用harbor仓库 修改每个node上的docker认证仓库 将每个node节点上的docker同样需要配置可信任仓库 vi /etc/docker/daemon.json #修改为 {& ...