当前位置:网站首页>随机数生成的四种方法|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();
边栏推荐
- 微波雷达感应模块技术,实时智能检测人体存在,静止微小动静感知
- Django uses mysqlclient service to connect and write to the database
- third-party dynamic library (libcudnn.so) that Paddle depends on is not configured correctl
- Is it safe to open a mobile stock account? Is it reliable?
- 通过POI追加数据到excel中小案例
- MMO project learning 1: preheating
- What does software testing do? What are the requirements for learning?
- 众昂矿业:2022年全球萤石行业市场供给现状分析
- [C language] string function and Simulation Implementation strlen & strcpy & strcat & StrCmp
- The binary string mode is displayed after the value with the field type of longtext in MySQL is exported
猜你喜欢

MMO项目学习一:预热

Notion 类生产力工具如何选择?Notion 、FlowUs 、Wolai 对比评测

The relationship between temperature measurement and imaging accuracy of ifd-x micro infrared imager (module)

40000 word Wenshuo operator new & operator delete

C application interface development foundation - form control (5) - grouping control

Bitcoinwin (BCW) was invited to attend Hanoi traders fair 2022

面试官:Redis中集合数据类型的内部实现方式是什么?

Win10 x64环境下基于VS2017和cmake-gui配置使用zxing以及opencv,并实现data metrix码的简单检测

再忙不能忘安全

Force buckle 729 My schedule I
随机推荐
[untitled]
The relationship between temperature measurement and imaging accuracy of ifd-x micro infrared imager (module)
Django使用mysqlclient服务连接并写入数据库的操作过程
What are general items
[AI framework basic technology] automatic derivation mechanism (autograd)
手机股票开户安全吗?靠不靠谱啊?
aggregate
IBM has laid off 40 + year-old employees in a large area. Mastering these ten search skills will improve your work efficiency ten times
How to choose the notion productivity tools? Comparison and evaluation of notion, flowus and WOLAI
The binary string mode is displayed after the value with the field type of longtext in MySQL is exported
S7-200smart uses V90 Modbus communication control library to control the specific methods and steps of V90 servo
[Collection - industry solutions] how to build a high-performance data acceleration and data editing platform
深度学习 卷积神经网络(CNN)基础
国海证券在网上开户安全吗?
Android面试,android音视频开发
Zhongang Mining: analysis of the current market supply situation of the global fluorite industry in 2022
成功入职百度月薪35K,2022Android开发面试解答
MMO项目学习一:预热
C#应用程序界面开发基础——窗体控制(6)——菜单栏、工具栏和状态栏控件
Realizing deep learning framework from zero -- LSTM from theory to practice [practice]