当前位置:网站首页>Four methods of random number generation | random | math | threadlocalrandom | securityrandom
Four methods of random number generation | random | math | threadlocalrandom | securityrandom
2022-07-05 19:50:00 【Bulst】
List of articles
Random
Random Generate random number , There's a constructor , Delivered long Type value , When using empty constructs , In fact, what is conveyed is System.nanoTime() , That is, the value of milliseconds of the current time .
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);
}
}
Parameters are also called seeds . Generally, it is enough to use null parameter construction , If you specify the value of the seed , Then the generated random numbers are the same , So seeds cannot be written dead .
Java.util.Random The random algorithm implemented in class is pseudorandom , That is to say, random with rules , Is to give seed Randomly generated numbers in the interval of .
Of the same number of seeds Random object , The random numbers generated are exactly the same .
Random It's thread safe , Inside Seed Global variable , When multiple threads generate random numbers , Used cas .
private final AtomicLong seed;
Math.random()
More commonly used methods of generating random numbers , Default 0.0-1.0 Decimal between 【 Left closed right away 】
Its bottom layer calls Random Is a parameter - free construction method , And it cannot be set seed value .
// The underlying call Random Parameter free constructor
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 yes Java7 Newly added , For concurrent use of multiple threads , Velocity ratio Random Be quick .
ThreadLocalRandom It's through ThreadLocal Improved tool class for random generation , Each thread holds a separate , There is no competition
ThreadLocalRandom threadLocalRandom = ThreadLocalRandom.current();
int threadLocalRandomValue = threadLocalRandom.nextInt(10);
System.out.println(threadLocalRandomValue);
The construction method is private , It can only be in singleton mode , Cannot set seed.
SecurityRandom
SeurityRandom Built in two random number algorithms ,NativePrRNG and SHA1PRNG . adopt new To create , By default NavivePRNG The algorithm generates random numbers , It can also be modified through parameters .
It can also be done through getInstance To initialize the object , Just have a parameter passing algorithm name , The second parameter specifies the algorithm package ,SHA1PRNG Performance ratio of NATIVEPRNG Double the performance , You can set seed , If seed Fix , Then the random result will remain the same .
SecureRandom instance = SecureRandom.getInstanceStrong();
// SecureRandom instance = new SecureRandom();
int instanceValue = instance.nextInt(10);
System.out.println(instanceValue);
SecureRandom instance = SecureRandom.getInstance("SHA1PRNG");
// Set seed value , The result remains unchanged.
instance.setSeed(1);
int instanceValue = instance.nextInt(10);
System.out.println(instanceValue);
JvmRandom
It is essentially the same as Math.random() equally , It's also true Random A kind of , Realize in commons-lang It's a bag .
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();
边栏推荐
- Is the education of caiqiantang reliable and safe?
- Postman核心功能解析-参数化和测试报告
- XaaS 陷阱:万物皆服务(可能)并不是IT真正需要的东西
- 安卓面试宝典,2022Android面试笔试总结
- 浮动元素与父级、兄弟盒子的关系
- redis集群模拟消息队列
- No matter how busy you are, you can't forget safety
- Debezium series: parsing the default value character set
- That's awesome. It's enough to read this article
- 通过POI追加数据到excel中小案例
猜你喜欢
What does software testing do? What are the requirements for learning?
浅浅的谈一下ThreadLocalInsecureRandom
建议收藏,我的腾讯Android面试经历分享
UWB超宽带定位技术,实时厘米级高精度定位应用,超宽带传输技术
aggregate
JVMRandom不可设置种子|问题追溯|源码追溯
再忙不能忘安全
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
[FAQ] summary of common causes and solutions of Huawei account service error 907135701
The problem of returning the longtext field in MySQL and its solution
随机推荐
软件测试工程师是做什么的?待遇前景怎么样?
Two pits exported using easyexcel template (map empty data columns are disordered and nested objects are not supported)
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
建议收藏,我的腾讯Android面试经历分享
Win10 x64环境下基于VS2017和cmake-gui配置使用zxing以及opencv,并实现data metrix码的简单检测
【obs】libobs-winrt :CreateDispatcherQueueController
[Collection - industry solutions] how to build a high-performance data acceleration and data editing platform
[FAQ] summary of common causes and solutions of Huawei account service error 907135701
Millimeter wave radar human body sensor, intelligent perception of static presence, human presence detection application
Information / data
Interviewer: what is the internal implementation of set data types in redis?
XaaS 陷阱:万物皆服务(可能)并不是IT真正需要的东西
Complete interview questions for interviewers and senior Android engineers in front-line Internet enterprises
Thread pool parameters and reasonable settings
How about testing outsourcing companies?
JMeter 常用的几种断言方法,你会了吗?
Gstreamer中的task
太牛了,看这篇足矣了
国信证券在网上开户安全吗?
[untitled]