当前位置:网站首页>随机数生成的四种方法|Random|Math|ThreadLocalRandom|SecurityRandom
随机数生成的四种方法|Random|Math|ThreadLocalRandom|SecurityRandom
2022-07-05 19:46:00 【步尔斯特】
Random
Random 生成随机数,有一个构造函数,传递的 long 类型的值,当使用空构造的时候,实际上传递的是 System.nanoTime() ,即当前时间的毫秒数的值。
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);
}
}
参数也叫种子。一般情况下使用空参构造即可,如果指定种子的数值,则生成的随机数都是一样的,所以种子不能写死。
Java.util.Random 类中实现的随机算法是伪随机,也就是有规则的随机,就是在给定 seed 的区间内随机生成的数字。
相同种子数的Random 对象,产生的随机数是完全一样的。
Random 是线程安全的,内部的Seed 是全局变量,多线程生成随机数时,使用了 cas 。
private final AtomicLong seed;
Math.random()
比较常用的生成随机数的方法,默认 0.0-1.0 之间的小数【左闭右开】
其底层调用了Random的无参构造方法,并且不可设置seed值。
// 底层调用了Random无参构造函数
double mathRandom = Math.random();
System.out.println(mathRandom);
public static double random() {
return RandomNumberGeneratorHolder.randomNumberGenerator.nextDouble();
}
private static final class RandomNumberGeneratorHolder {
static final Random randomNumberGenerator = new Random();
}
ThreadLocalRandom
ThreadLocalRandom 是 Java7 新增的,给多线程并发使用的,速度比Random 要快。
ThreadLocalRandom 是通过 ThreadLocal 改进的用于随机生成的工具类,每个线程单独持有一个,不存在竞争问题
ThreadLocalRandom threadLocalRandom = ThreadLocalRandom.current();
int threadLocalRandomValue = threadLocalRandom.nextInt(10);
System.out.println(threadLocalRandomValue);
构造方法是 private ,只能是单例模式,不能够设置 seed。
SecurityRandom
SeurityRandom 内置两种随机数算法,NativePrRNG 和 SHA1PRNG 。通过 new 来创建,默认使用 NavivePRNG 算法生成随机数,也可以通过参数修改。
也可以通过 getInstance 来初始化对象,有一个参数传递算法名就可以,第二个参数指定算法程序包 ,SHA1PRNG 的性能比 NATIVEPRNG 性能好一倍,可以设置 seed ,如果seed 固定,那么随机的结果也将不变。
SecureRandom instance = SecureRandom.getInstanceStrong();
// SecureRandom instance = new SecureRandom();
int instanceValue = instance.nextInt(10);
System.out.println(instanceValue);
SecureRandom instance = SecureRandom.getInstance("SHA1PRNG");
// 设置种子值,结果不变
instance.setSeed(1);
int instanceValue = instance.nextInt(10);
System.out.println(instanceValue);
JvmRandom
其本质上和Math.random()一样,也是对Random 的一种,实现在 commons-lang 包下。
JVMRandom jvmRandom = new JVMRandom();
int i = jvmRandom.nextInt(10);
System.out.println(i);
public int nextInt(int n) {
return SHARED_RANDOM.nextInt(n);
}
private static final Random SHARED_RANDOM = new Random();
边栏推荐
- 国海证券在网上开户安全吗?
- What do software test engineers do? How about the prospect of treatment?
- Notion 类生产力工具如何选择?Notion 、FlowUs 、Wolai 对比评测
- Add data to excel small and medium-sized cases through poi
- Complete interview questions for interviewers and senior Android engineers in front-line Internet enterprises
- Hiengine: comparable to the local cloud native memory database engine
- UWB超宽带定位技术,实时厘米级高精度定位应用,超宽带传输技术
- 【obs】libobs-winrt :CreateDispatcherQueueController
- 40000 word Wenshuo operator new & operator delete
- What is the core value of testing?
猜你喜欢

【obs】QString的UTF-8中文转换到blog打印 UTF-8 char*

Zhongang Mining: analysis of the current market supply situation of the global fluorite industry in 2022
Django使用mysqlclient服务连接并写入数据库的操作过程

使用easyexcel模板导出的两个坑(Map空数据列错乱和不支持嵌套对象)

third-party dynamic library (libcudnn.so) that Paddle depends on is not configured correctl

XaaS 陷阱:万物皆服务(可能)并不是IT真正需要的东西

C#应用程序界面开发基础——窗体控制(5)——分组类控件

Necessary skills for interview in large factories, 2022android will not die, I will not fall

关于 Notion-Like 工具的反思和畅想

Fuzor 2020 software installation package download and installation tutorial
随机推荐
Fuzor 2020軟件安裝包下載及安裝教程
Complete interview questions for interviewers and senior Android engineers in front-line Internet enterprises
Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL
[OBS] qstring's UTF-8 Chinese conversion to blog printing UTF-8 char*
【obs】libobs-winrt :CreateDispatcherQueueController
四万字长文说operator new & operator delete
JMeter 常用的几种断言方法,你会了吗?
力扣 1200. 最小绝对差
okcc呼叫中心有什么作用
众昂矿业:2022年全球萤石行业市场供给现状分析
MMO project learning 1: preheating
MySQL中字段类型为longtext的值导出后显示二进制串方式
40000 word Wenshuo operator new & operator delete
Concept and syntax of function
Redis cluster simulated message queue
How to convert word into PDF? Word to PDF simple way to share!
Summer Challenge database Xueba notes, quick review of exams / interviews~
软件测试是干什么的?学习有啥要求?
使用 RepositoryProvider简化父子组件的传值
司空见惯 - 英雄扫雷鼠