当前位置:网站首页>Jvmrandom cannot set seeds | problem tracing | source code tracing
Jvmrandom cannot set seeds | problem tracing | source code tracing
2022-07-05 19:50:00 【Bulst】
Last article , We introduce several generation strategies of random numbers .
So next , Let's analyze JVMRandom The problem is .
When we normally construct a random number as follows , There is no problem .
JVMRandom jvmRandom = new JVMRandom();
int i = jvmRandom.nextInt(10);
System.out.println(i);
since JVMRandom The underlying call is Random Constructor for , Can it also be set seed Is it worth it ?
So let's verify that
JVMRandom jvmRandom = new JVMRandom();
jvmRandom.setSeed(2);
int i = jvmRandom.nextInt(10);
System.out.println(i);
Shua ~ Wrong report
Exception in thread "main" java.lang.UnsupportedOperationException
at org.apache.commons.lang.math.JVMRandom.setSeed(JVMRandom.java:71)
at com.ossa.producer.controller.ProducerController.main(ProducerController.java:61)
Let's take a look at the source code , To find out .
JVMRandom Inherited Random, Rewrite the setSeed Method .
And setting seed value is no longer supported .
Source code is as follows , When constructed The value is false when , Will throw an exception .
/** * Unsupported in 2.0. * * @param seed ignored * @throws UnsupportedOperationException */
public synchronized void setSeed(long seed) {
if (this.constructed) {
throw new UnsupportedOperationException();
}
}
Go back to constructed value , The default is false
/** * Ensures that only the parent constructor can call reseed. */
private boolean constructed = false;
however , By observing its structure, we can find , It has only one parameterless constructor .
Let's see what it looks like
/** * Constructs a new instance. */
public JVMRandom() {
this.constructed = true;
}
Oh , Then there's no way out , in other words , If we want to use it JVMRandom To construct random numbers , The object must be created through the parameterless constructor , that constructed It must be true, therefore , If you call setSeed Method , Then the above exception will be thrown .
边栏推荐
- aggregate
- Is it safe for Guohai Securities to open an account online?
- [untitled]
- gst-launch的-v参数
- How to convert word into PDF? Word to PDF simple way to share!
- 【obs】libobs-winrt :CreateDispatcherQueueController
- [OBS] qstring's UTF-8 Chinese conversion to blog printing UTF-8 char*
- [FAQ] summary of common causes and solutions of Huawei account service error 907135701
- PHP利用ueditor实现上传图片添加水印
- Worthy of being a boss, byte Daniel spent eight months on another masterpiece
猜你喜欢
众昂矿业:2022年全球萤石行业市场供给现状分析
Microwave radar induction module technology, real-time intelligent detection of human existence, static micro motion and static perception
Do you know several assertion methods commonly used by JMeter?
ACM getting started Day1
Hiengine: comparable to the local cloud native memory database engine
【合集- 行业解决方案】如何搭建高性能的数据加速与数据编排平台
40000 word Wenshuo operator new & operator delete
安卓面试宝典,2022Android面试笔试总结
What do software test engineers do? How about the prospect of treatment?
Django uses mysqlclient service to connect and write to the database
随机推荐
浅浅的谈一下ThreadLocalInsecureRandom
如何安全快速地从 Centos迁移到openEuler
UWB超宽带定位技术,实时厘米级高精度定位应用,超宽带传输技术
爬虫练习题(二)
IBM has laid off 40 + year-old employees in a large area. Mastering these ten search skills will improve your work efficiency ten times
测试外包公司怎么样?
测试的核心价值到底是什么?
[C language] string function and Simulation Implementation strlen & strcpy & strcat & StrCmp
Django uses mysqlclient service to connect and write to the database
Is it safe for Guohai Securities to open an account online?
浮动元素与父级、兄弟盒子的关系
完爆面试官,一线互联网企业高级Android工程师面试题大全
四万字长文说operator new & operator delete
Float.floatToRawIntBits的返回值具体意思,将float转为byte数组
Zhongang Mining: analysis of the current market supply situation of the global fluorite industry in 2022
Which securities company is better and which platform is safer for mobile account opening
从零实现深度学习框架——LSTM从理论到实战【实战】
面试官:Redis中集合数据类型的内部实现方式是什么?
The binary string mode is displayed after the value with the field type of longtext in MySQL is exported
随机数生成的四种方法|Random|Math|ThreadLocalRandom|SecurityRandom