当前位置:网站首页>随机数生成的四种方法|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();
边栏推荐
- 5 years of experience, 27 days of Android programmer interview, 2022 programmer advanced classic
- How about testing outsourcing companies?
- openh264解码数据流向分析
- 力扣 1200. 最小绝对差
- C#应用程序界面开发基础——窗体控制(5)——分组类控件
- The problem of returning the longtext field in MySQL and its solution
- What are general items
- 打新债在哪里操作开户是更安全可靠的呢
- Inventory of the most complete low code / no code platforms in the whole network: Jiandao cloud, partner cloud, Mingdao cloud, Qingliu, xurong cloud, Jijian cloud, treelab, nailing · Yida, Tencent clo
- Is it safe for China Galaxy Securities to open an account? Securities account opening
猜你喜欢
40000 word Wenshuo operator new & operator delete
Fuzor 2020軟件安裝包下載及安裝教程
【硬核干货】数据分析哪家强?选Pandas还是选SQL
力扣 729. 我的日程安排表 I
Password reset of MariaDB root user and ordinary user
安卓面试宝典,2022Android面试笔试总结
Redis cluster simulated message queue
城链科技数字化创新战略峰会圆满召开
Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL
C application interface development foundation - form control (5) - grouping control
随机推荐
third-party dynamic library (libcudnn.so) that Paddle depends on is not configured correctl
Postman核心功能解析-参数化和测试报告
Is it safe to open a mobile stock account? Is it reliable?
Float.floatToRawIntBits的返回值具体意思,将float转为byte数组
Concept and syntax of function
测试外包公司怎么样?
webuploader文件上传 拖拽上传 进度监听 类型控制 上传结果监听控件
从零实现深度学习框架——LSTM从理论到实战【实战】
深度學習 卷積神經網絡(CNN)基礎
[AI framework basic technology] automatic derivation mechanism (autograd)
Fundamentals of machine learning (III) -- KNN / naive Bayes / cross validation / grid search
力扣 1200. 最小绝对差
通配符选择器
打新债在哪里操作开户是更安全可靠的呢
手机股票开户安全吗?靠不靠谱啊?
Hiengine: comparable to the local cloud native memory database engine
id选择器和类选择器的区别
S7-200SMART利用V90 MODBUS通信控制库控制V90伺服的具体方法和步骤
Gstreamer中的task
【obs】libobs-winrt :CreateDispatcherQueueController