当前位置:网站首页>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 .
边栏推荐
- 城链科技数字化创新战略峰会圆满召开
- Tasks in GStreamer
- 秋招字节面试官问你还有什么问题?其实你已经踩雷了
- 微波雷达感应模块技术,实时智能检测人体存在,静止微小动静感知
- C application interface development foundation - form control (5) - grouping control
- okcc呼叫中心有什么作用
- [hard core dry goods] which company is better in data analysis? Choose pandas or SQL
- 线程池参数及合理设置
- The binary string mode is displayed after the value with the field type of longtext in MySQL is exported
- 软件测试工程师是做什么的?待遇前景怎么样?
猜你喜欢
UWB超宽带定位技术,实时厘米级高精度定位应用,超宽带传输技术
软件测试工程师是做什么的?待遇前景怎么样?
测试外包公司怎么样?
不愧是大佬,字节大牛耗时八个月又一力作
Successful entry into Baidu, 35K monthly salary, 2022 Android development interview answer
面试官:Redis中集合数据类型的内部实现方式是什么?
使用 RepositoryProvider简化父子组件的传值
完爆面试官,一线互联网企业高级Android工程师面试题大全
IBM has laid off 40 + year-old employees in a large area. Mastering these ten search skills will improve your work efficiency ten times
Win10 x64环境下基于VS2017和cmake-gui配置使用zxing以及opencv,并实现data metrix码的简单检测
随机推荐
Do you know several assertion methods commonly used by JMeter?
信息/数据
[untitled]
95后阿里P7晒出工资单:狠补了这个,真香...
CADD课程学习(7)-- 模拟靶点和小分子相互作用 (半柔性对接 AutoDock)
okcc呼叫中心有什么作用
40000 word Wenshuo operator new & operator delete
How MySQL queries and modifies JSON data
如何在2022年更明智地应用智能合约?
openh264解码数据流向分析
成功入职百度月薪35K,2022Android开发面试解答
Hiengine: comparable to the local cloud native memory database engine
力扣 1200. 最小绝对差
Using repositoryprovider to simplify the value passing of parent-child components
集合
安卓面试宝典,2022Android面试笔试总结
【无标题】
爬虫练习题(二)
Hiengine: comparable to the local cloud native memory database engine
大厂面试必备技能,2022Android不死我不倒