当前位置:网站首页>随机数生成的四种方法|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();
边栏推荐
- [untitled]
- Android面试,android音视频开发
- 软件测试是干什么的?学习有啥要求?
- Base du réseau neuronal de convolution d'apprentissage profond (CNN)
- Two pits exported using easyexcel template (map empty data columns are disordered and nested objects are not supported)
- Postman core function analysis - parameterization and test report
- 通过POI追加数据到excel中小案例
- Multi branch structure
- 常用运算符与运算符优先级
- Summer Challenge database Xueba notes, quick review of exams / interviews~
猜你喜欢
Mysql如何对json数据进行查询及修改
【硬核干货】数据分析哪家强?选Pandas还是选SQL
Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL
MMO project learning 1: preheating
Bitcoinwin (BCW) was invited to attend Hanoi traders fair 2022
Fuzor 2020軟件安裝包下載及安裝教程
Worthy of being a boss, byte Daniel spent eight months on another masterpiece
third-party dynamic library (libcudnn.so) that Paddle depends on is not configured correctl
2022 the latest big company Android interview real problem analysis, Android development will be able to technology
强化学习-学习笔记4 | Actor-Critic
随机推荐
Force buckle 729 My schedule I
gst-launch的-v参数
How to realize the Online timer and offline timer in the game
acm入门day1
JMeter 常用的几种断言方法,你会了吗?
通配符选择器
Notion 类生产力工具如何选择?Notion 、FlowUs 、Wolai 对比评测
常用运算符与运算符优先级
司空见惯 - 英雄扫雷鼠
c——顺序结构
Millimeter wave radar human body sensor, intelligent perception of static presence, human presence detection application
openh264解码数据流向分析
What does software testing do? What are the requirements for learning?
shell编程基础(第9篇:循环)
再忙不能忘安全
力扣 1200. 最小绝对差
城链科技数字化创新战略峰会圆满召开
MySQL中字段类型为longtext的值导出后显示二进制串方式
Fuzor 2020软件安装包下载及安装教程
Apprentissage du projet MMO I: préchauffage