代码评审——随机数Random问题

问题描述:

为了获取唯一值,经常会依赖产生随机数来保证唯一性。在获取随机数时,如果使用错误的方法,会比较低效。

可以参考以下代码:

java 复制代码
public static String geneRundomNo(){
    Random r=new Random();
    int num=r.nextInt(100000);
    return "" + num;
}

此时,使用静态代码扫描工具,会出现以下提示:

Save and re-use this "Random".


原因分析:

可以看下来自静态代码扫描工具的说明:

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.

简单的说就是每次需要一个随机值时创建一个新的Random对象是低效的,并且可能会根据JDK的版本不同产生非随机的数字。


解决方案:

首先看下静态代码扫描工具给出的建议。

java 复制代码
private Random rand = SecureRandom.getInstanceStrong();  // SecureRandom is preferred to Random

public void doSomethingCommon() {
  int rValue = this.rand.nextInt();
  //...

如果按照这个建议执行,那么会产生2个问题

1、按照上述方式引入,还需要解决构建函数的问题。

当引入java.security.SecureRandom后,需要解决构造函数问题。

可以通过以下方式解决:

java 复制代码
private static Random rand;
static {
    try {
        rand = SecureRandom.getInstanceStrong();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
}

2、使用SecureRandom.getInstanceStrong()会导致线程阻塞问题,这个问题也是最严重的。

原因 :不同的系统环境执行的底层代码不相同,在linux系统中是通过底层NativePRNG方法,通过/dev/random方式读取随机数,/dev/random方式受系统环境的影响容易造成线程阻塞,在windows系统中通过generateSeed的native方法读取,不会阻塞线程。具体的分析过程可以参考《SecureRandom.getInstanceStrong()引发的线程阻塞问题分析》。这篇文章写的非常详细了。

解决方案 :使用new SecureRandom()替换SecureRandom.getInstanceStrong()。
请注意:这种解决方案是伪随机。

其他相关知识点:

以下内容摘自文章《SecureRandom 引发的线程阻塞》

  1. SecureRandom本身并不是伪随机算法的实现,而是使用了其他类提供的算法来获取伪随机数。

  2. 如果简单的new一个SecureRandom对象的话,在不同的操作平台会获取到不同的算法,windows默认是SHA1PRNG,Linux的话是NativePRNG。

  3. Linux下的NativePRNG,如果调用generateSeed()方法,这个方法会读取Linux系统的/dev/random文件,这个文件在JAVA_HOME/jre/lib/securiy/java.security里面有默认定义。而/dev/random文件是动态生成的,如果没有数据,就会阻塞。也就造成了第一个现象。

  4. 可以使用-Djava.security.egd=file:/dev/./urandom (这个文件名多个u)强制使用/dev/urandom这个文件,避免阻塞现象。中间加个点的解释是因为某个JDK BUG,SO那个帖子有链接。

  5. 如果使用SecureRandom.getInstanceStrong()这种方法初始化SecureRandom对象的话,会使用NativePRNGBlocking这个算法,而NativePRNGBlocking算法的特性如下:

    NativePRNGBlocking uses /dev/random for all of the following operations:

    Initial seeding: This initializes an internal SHA1PRNG instance using 20 bytes from /dev/random

    Calls to nextBytes(), nextInt(), etc.: This provides the XOR of the output from the internal SHA1PRNG instance (see above) and data read from /dev/random

    Calls to getSeed(): This provides data read from /dev/random

    可见这个算法完全依赖/dev/random,所以当这个文件随机数不够的时候,自然会导致卡顿了。

  6. 如果使用NativePRNGBlocking算法的话,4中的系统参数失效!!!

  7. 一般使用SecureRandom不需要设置Seed,不需要设置算法,使用默认的,甚至一个静态的即可,如果有需求的话可以在运行一段时间后setSeed一下

参考文章:《一文详解安全随机数》这篇文章详细介绍了如何使用安全随机数。

如果这篇博客对大家有所帮助,我希望能得到各位的免费点赞收藏,作为对我的鼓励和支持。

同时,也请大家在评论区留下您宝贵的意见和建议,我将非常欢迎。

感谢大家的支持评论收藏!!!

相关推荐
煸橙干儿~~4 分钟前
分析JS Crash(进程崩溃)
java·前端·javascript
2401_854391085 分钟前
Spring Boot大学生就业招聘系统的开发与部署
java·spring boot·后端
Amor风信子6 分钟前
华为OD机试真题---跳房子II
java·数据结构·算法
杨荧32 分钟前
【JAVA开源】基于Vue和SpringBoot的洗衣店订单管理系统
java·开发语言·vue.js·spring boot·spring cloud·开源
陈逸轩*^_^*1 小时前
Java 网络编程基础
java·网络·计算机网络
这孩子叫逆1 小时前
Spring Boot项目的创建与使用
java·spring boot·后端
星星法术嗲人1 小时前
【Java】—— 集合框架:Collections工具类的使用
java·开发语言
一丝晨光1 小时前
C++、Ruby和JavaScript
java·开发语言·javascript·c++·python·c·ruby
天上掉下来个程小白1 小时前
Stream流的中间方法
java·开发语言·windows
xujinwei_gingko2 小时前
JAVA基础面试题汇总(持续更新)
java·开发语言