Java 随机数 Random VS SecureRandom
1. Math.random() 静态方法
产生的随机数是 0 - 1 之间的一个 double,即 0 <= random <= 1。
使用:
for (int i = 0; i < 10; i++) {
System.out.println(Math.random());
}
结果:
0.3598613895606426
0.2666778145365811
0.25090731064243355
0.011064998061666276
0.600686228175639
0.9084006027629496
0.12700524654847833
0.6084605849069343
0.7290804782514261
0.9923831908303121
实现原理:
When this method is first called, it creates a single new pseudorandom-number generator, exactly as if by the expression new java.util.Random()
This new pseudorandom-number generator is used thereafter for all calls to this method and is used nowhere else.
当第一次调用 Math.random() 方法时,自动创建了一个伪随机数生成器,实际上用的是 new java.util.Random()。
当接下来继续调用 Math.random() 方法时,就会使用这个新的伪随机数生成器。
源码如下:
public static double random() {
Random rnd = randomNumberGenerator;
if (rnd == null) rnd = initRNG(); // 第一次调用,创建一个伪随机数生成器
return rnd.nextDouble();
}
private static synchronized Random initRNG() {
Random rnd = randomNumberGenerator;
return (rnd == null) ? (randomNumberGenerator = new Random()) : rnd; // 实际上用的是new java.util.Random()
}
This method is properly synchronized to allow correct use by more than one thread. However, if many threads need to generate pseudorandom numbers at a great rate, it may reduce contention for each thread to have its own pseudorandom-number generator.
initRNG() 方法是 synchronized 的,因此在多线程情况下,只有一个线程会负责创建伪随机数生成器(使用当前时间作为种子),其他线程则利用该伪随机数生成器产生随机数。
因此 Math.random() 方法是线程安全的。
什么情况下随机数的生成线程不安全:
- 线程1在第一次调用
random()时产生一个生成器generator1,使用当前时间作为种子。 - 线程2在第一次调用
random()时产生一个生成器generator2,使用当前时间作为种子。 - 碰巧
generator1和generator2使用相同的种子,导致generator1以后产生的随机数每次都和generator2以后产生的随机数相同。
什么情况下随机数的生成线程安全: Math.random() 静态方法使用
- 线程1在第一次调用
random()时产生一个生成器generator1,使用当前时间作为种子。 - 线程2在第一次调用
random()时发现已经有一个生成器generator1,则直接使用生成器generator1。
public class JavaRandom {
public static void main(String args[]) {
new MyThread().start();
new MyThread().start();
}
}
class MyThread extends Thread {
public void run() {
for (int i = 0; i < 2; i++) {
System.out.println(Thread.currentThread().getName() + ": " + Math.random());
}
}
}
结果:
Thread-1: 0.8043581595645333
Thread-0: 0.9338269554390357
Thread-1: 0.5571569413128877
Thread-0: 0.37484586843392464
2. java.util.Random 工具类
基本算法:linear congruential pseudorandom number generator (LGC) 线性同余法伪随机数生成器
缺点:可预测
An attacker will simply compute the seed from the output values observed. This takes significantly less time than 2^48 in the case of java.util.Random.
从输出中可以很容易计算出种子值。
It is shown that you can predict future Random outputs observing only two(!) output values in time roughly 2^16.
因此可以预测出下一个输出的随机数。
You should never use an LCG for security-critical purposes.
在注重信息安全的应用中,不要使用 LCG 算法生成随机数,请使用 SecureRandom。
使用:
Random random = new Random();
for (int i = 0; i < 5; i++) {
System.out.println(random.nextInt());
}
结果:
-24520987
-96094681
-952622427
300260419
1489256498
Random类默认使用当前系统时钟作为种子:
public Random() {
this(seedUniquifier() ^ System.nanoTime());
}
public Random(long seed) {
if (getClass() == Random.class)
this.seed = new AtomicLong(initialScramble(seed));
else {
// subclass might have overriden setSeed
this.seed = new AtomicLong();
setSeed(seed);
}
}
Random类提供的方法:API
nextBoolean()- 返回均匀分布的true或者falsenextBytes(byte[] bytes)nextDouble()- 返回 0.0 到 1.0 之间的均匀分布的doublenextFloat()- 返回 0.0 到 1.0 之间的均匀分布的floatnextGaussian()- 返回 0.0 到 1.0 之间的高斯分布(即正态分布)的doublenextInt()- 返回均匀分布的intnextInt(int n)- 返回 0 到 n 之间的均匀分布的int(包括 0,不包括 n)nextLong()- 返回均匀分布的longsetSeed(long seed)- 设置种子
只要种子一样,产生的随机数也一样: 因为种子确定,随机数算法也确定,因此输出是确定的!
Random random1 = new Random(10000);
Random random2 = new Random(10000);
for (int i = 0; i < 5; i++) {
System.out.println(random1.nextInt() + " = " + random2.nextInt());
}
结果:
-498702880 = -498702880
-858606152 = -858606152
1942818232 = 1942818232
-1044940345 = -1044940345
1588429001 = 1588429001
3. java.util.concurrent.ThreadLocalRandom 工具类
ThreadLocalRandom 是 JDK 7 之后提供,也是继承至 java.util.Random。
private static final ThreadLocal<ThreadLocalRandom> localRandom =
new ThreadLocal<ThreadLocalRandom>() {
protected ThreadLocalRandom initialValue() {
return new ThreadLocalRandom();
}
};
每一个线程有一个独立的随机数生成器,用于并发产生随机数,能够解决多个线程发生的竞争争夺。效率更高!ThreadLocalRandom 不是直接用 new 实例化,而是第一次使用其静态方法 current() 得到 ThreadLocal<ThreadLocalRandom> 实例,然后调用 java.util.Random 类提供的方法获得各种随机数。
使用:
public class JavaRandom {
public static void main(String args[]) {
new MyThread().start();
new MyThread().start();
}
}
class MyThread extends Thread {
public void run() {
for (int i = 0; i < 2; i++) {
System.out.println(Thread.currentThread().getName() + ": " + ThreadLocalRandom.current().nextDouble());
}
}
}
结果:
Thread-0: 0.13267085355389086
Thread-1: 0.1138484950410098
Thread-0: 0.17187774671469858
Thread-1: 0.9305225910262372
4. java.Security.SecureRandom
也是继承至 java.util.Random。
Instances of java.util.Random are not cryptographically secure. Consider instead using SecureRandom to get a cryptographically secure pseudo-random number generator for use by security-sensitive applications.
SecureRandom takes Random Data from your os (they can be interval between keystrokes etc - most os collect these data store them in files - /dev/random and /dev/urandom in case of linux/solaris) and uses that as the seed.
操作系统收集了一些随机事件,比如鼠标点击,键盘点击等等,SecureRandom 使用这些随机事件作为种子。
SecureRandom 提供加密的强随机数生成器 (RNG),要求种子必须是不可预知的,产生非确定性输出。SecureRandom 也提供了与实现无关的算法,因此,调用方(应用程序代码)会请求特定的 RNG 算法并将它传回到该算法的 SecureRandom 对象中。
如果仅指定算法名称,如下所示:
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");如果既指定了算法名称又指定了包提供程序,如下所示:
SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");
使用:
SecureRandom random1 = SecureRandom.getInstance("SHA1PRNG");
SecureRandom random2 = SecureRandom.getInstance("SHA1PRNG");
for (int i = 0; i < 5; i++) {
System.out.println(random1.nextInt() + " != " + random2.nextInt());
}
结果:
704046703 != 2117229935
60819811 != 107252259
425075610 != -295395347
682299589 != -1637998900
-1147654329 != 1418666937
5. 随机字符串
可以使用 Apache Commons-Lang 包中的 RandomStringUtils 类。
Maven 依赖如下:
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
示例:
public class RandomStringDemo {
public static void main(String[] args) {
// Creates a 64 chars length random string of number.
String result = RandomStringUtils.random(64, false, true);
System.out.println("random = " + result);
// Creates a 64 chars length of random alphabetic string.
result = RandomStringUtils.randomAlphabetic(64);
System.out.println("random = " + result);
// Creates a 32 chars length of random ascii string.
result = RandomStringUtils.randomAscii(32);
System.out.println("random = " + result);
// Creates a 32 chars length of string from the defined array of
// characters including numeric and alphabetic characters.
result = RandomStringUtils.random(32, 0, 20, true, true, "qw32rfHIJk9iQ8Ud7h0X".toCharArray());
System.out.println("random = " + result);
}
}
RandomStringUtils 类的实现上也是依赖了 java.util.Random 工具类:

引用:
http://yangzb.iteye.com/blog/325264
Difference between java.util.Random and java.security.SecureRandom
Java 随机数 Random VS SecureRandom的更多相关文章
- 真伪随机数 ——Random和SecureRandom
Random Random用来创建伪随机数.所谓伪随机数,是指只要给定一个初始的种子,产生的随机数序列是完全一样的. 要生成一个随机数,可以使用nextInt().nextLong().nextFlo ...
- 实现java随机数Random的几招
一,在java.util这个包里面提供了一个Random的类,我们可以新建一个Random的对象来产生随机数,可以产生随机整数.随机float.随机double,随机long,这个也是我们经常用的一个 ...
- 随机数Random和SecureRandom
"Random" objects should be reused Bug Critical Main sources owasp-a6 Available SinceNov 16 ...
- Java如何生成随机数 - Random、ThreadLocalRandom、SecureRandom
Java7 的Random伪随机数和线程安全的ThreadLocalRandom 一.Random伪随机数: Random 类专门用于生成一个伪随机数,它有两个构造器: 一个构造器使用默认的种子(以当 ...
- java java.uitl.Random产生随机数
通过使用java.uitl.Random产生一个1-10内的随机数.例: Random random = new Random(); int i = Math.abs(random.nextInt() ...
- Java – Generate random integers in a rangejava获取某个范围内的一个随机数
In this article, we will show you three ways to generate random integers in a range. java.util.Rando ...
- java的random生成某个范围内的随机数
import java.util.Random; /** * @author HP * @date 2019/4/16 */ public class randomTest { public stat ...
- Java随机数
本章先讲解Java随机数的几种产生方式,然后通过示例对其进行演示. 广义上讲,Java中的随机数的有三种产生方式:(01). 通过System.currentTimeMillis()来获取一个当前时间 ...
- 硬核 - Java 随机数相关 API 的演进与思考(上)
本系列将 Java 17 之前的随机数 API 以及 Java 17 之后的统一 API 都做了比较详细的说明,并且将随机数的特性以及实现思路也做了一些简单的分析,帮助大家明白为何会有这么多的随机数算 ...
- 硬核 - Java 随机数相关 API 的演进与思考(下)
本系列将 Java 17 之前的随机数 API 以及 Java 17 之后的统一 API 都做了比较详细的说明,并且将随机数的特性以及实现思路也做了一些简单的分析,帮助大家明白为何会有这么多的随机数算 ...
随机推荐
- vue前端开发仿钉图系列(1)高德地图的使用详解
最近公司让参考钉图做图层模块相关的功能,很庆幸有机会细细研究地图相关的东西.因为手机端用的是高德地图,web端也使用高德地图.还是和往常一样,先贴上效果图. 步骤1.在高德开放平台注册信息,创建自己的 ...
- .NET 白板书写预测-曲线拟合
白板软件书写速度是其最核心的功能,注册StylusPlugin从触摸线程拿触摸点数据并在另一UI线程绘制渲染是比较稳妥的方案,具体的可以查看小伙伴德熙的2019-1-28-WPF-高性能笔 - lin ...
- 什么是Streamlit
最近,我在数据分析的一些任务中尝试了闻名已久的Streamlit,再一次感受到Python的强大之处. 于是,准备根据自己的掌握情况,写一个介绍Streamlit的系列. 本文作为第一篇, 先介绍介绍 ...
- mac 系统使用vscode 创建c/c++ 工程项目 并配置断点调试
mac 使用vsCode 创建c/c++ 工程项目 并配置断点调试 使用vscode 创建c/c++工程项目 准备工作 使用 vscode 下载插件 C/C++ Project Generator 开 ...
- IntelliJ IDEA 2024.1 安装激活 (亲测有效!)
第一步:下载 IDEA 安装包 访问 IDEA 官网,下载 IDEA 2024.1.4 版本的安装包,下载链接如下 : idea官方链接 也可以在这里点击下载idea下载idea 第二步: 安装 ID ...
- CF#795 D - Max GEQ Sum O(n)做法
D - Max GEQ Sum input 3 4 -1 1 -1 2 5 -1 2 -3 2 -1 3 2 3 -1 output YES YES NO 题目大意:给你一个数组a,让你任意一个字段均 ...
- 重温c语言之,7天开整,就是随便的写写,第一天
一:转义字符 \t是一个字符,在printf里面,只占一个位置: 其他什么的抽象字符,用一个'\'+抽象字符就可以出现: \ddd这个是8进制的,可以转成10进制的,之后参考ASCLL码表即可 二:枚 ...
- 强化学习环境gym/gymnasium下的atari环境的v0、v4、v5的说明
声明: 本文是最新版gym-0.26.2下Atari环境的安装以及环境版本v0,v4,v5的说明的部分更新和汇总,可以看作是更新和延续版本. 由于gym已经由openai公司独立出来,虽然开发团队和投 ...
- 2.11 Linux四种远程管理协议
提到远程管理,通常指的是远程管理服务器,而非个人计算机.个人计算机可以随时拿来用,服务器通常放置在机房中,用户无法直接接触到服务器硬件,只能采用远程管理的方式. 远程管理,实际上就是计算机(服务器)之 ...
- 2.8 忘记root账户密码怎么办?
Linux 的单用户模式有些类似 Windows 的安全模式,只启动最少的程序用于系统修复.在单用户模式(运行级别为 1)中,Linux 引导进入根 shell,网络被禁用,只有少数进程运行. 单用户 ...