当前位置:网站首页>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 .
边栏推荐
- UWB超宽带定位技术,实时厘米级高精度定位应用,超宽带传输技术
- 95后阿里P7晒出工资单:狠补了这个,真香...
- 大厂面试必备技能,2022Android不死我不倒
- How about testing outsourcing companies?
- [FAQ] summary of common causes and solutions of Huawei account service error 907135701
- 手机股票开户安全吗?靠不靠谱啊?
- Reinforcement learning - learning notes 4 | actor critical
- 【obs】libobs-winrt :CreateDispatcherQueueController
- Gstreamer中的task
- 测试外包公司怎么样?
猜你喜欢
JVMRandom不可设置种子|问题追溯|源码追溯
力扣 1200. 最小绝对差
如何安全快速地从 Centos迁移到openEuler
That's awesome. It's enough to read this article
[untitled]
Hiengine: comparable to the local cloud native memory database engine
【无标题】
PHP uses ueditor to upload pictures and add watermarks
redis集群模拟消息队列
Necessary skills for interview in large factories, 2022android will not die, I will not fall
随机推荐
MMO项目学习一:预热
Float. The specific meaning of the return value of floattorawintbits is to convert float into byte array
Build your own website (16)
Wildcard selector
使用easyexcel模板导出的两个坑(Map空数据列错乱和不支持嵌套对象)
That's awesome. It's enough to read this article
Is it safe for Guohai Securities to open an account online?
What are general items
Reinforcement learning - learning notes 4 | actor critical
C#应用程序界面开发基础——窗体控制(5)——分组类控件
MMO project learning 1: preheating
安卓面试宝典,2022Android面试笔试总结
Webuploader file upload drag upload progress monitoring type control upload result monitoring control
UWB ultra wideband positioning technology, real-time centimeter level high-precision positioning application, ultra wideband transmission technology
Millimeter wave radar human body sensor, intelligent perception of static presence, human presence detection application
S7-200smart uses V90 Modbus communication control library to control the specific methods and steps of V90 servo
深度學習 卷積神經網絡(CNN)基礎
Shell编程基础(第8篇:分支语句-case in)
Two pits exported using easyexcel template (map empty data columns are disordered and nested objects are not supported)
全网最全的低代码/无代码平台盘点:简道云、伙伴云、明道云、轻流、速融云、集简云、Treelab、钉钉·宜搭、腾讯云·微搭、智能云·爱速搭、百数云