当前位置:网站首页>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 .
边栏推荐
- 安信证券在网上开户安全吗?
- 如何在2022年更明智地应用智能合约?
- 成功入职百度月薪35K,2022Android开发面试解答
- third-party dynamic library (libcudnn.so) that Paddle depends on is not configured correctl
- Is it safe for Anxin securities to open an account online?
- 函数的概念及语法
- Using repositoryprovider to simplify the value passing of parent-child components
- 力扣 1200. 最小绝对差
- MMO project learning 1: preheating
- 从零实现深度学习框架——LSTM从理论到实战【实战】
猜你喜欢
测试外包公司怎么样?
成功入职百度月薪35K,2022Android开发面试解答
How to apply smart contracts more wisely in 2022?
力扣 729. 我的日程安排表 I
使用 RepositoryProvider简化父子组件的传值
Android interview classic, 2022 Android interview written examination summary
C application interface development foundation - form control (5) - grouping control
ACM getting started Day1
司空见惯 - 英雄扫雷鼠
Password reset of MariaDB root user and ordinary user
随机推荐
What are general items
【obs】libobs-winrt :CreateDispatcherQueueController
Force buckle 729 My schedule I
Force buckle 1200 Minimum absolute difference
Common operators and operator priority
期货如何网上开户?安不安全?
城链科技数字化创新战略峰会圆满召开
Interviewer: what is the internal implementation of set data types in redis?
[OBS] qstring's UTF-8 Chinese conversion to blog printing UTF-8 char*
Millimeter wave radar human body sensor, intelligent perception of static presence, human presence detection application
40000 word Wenshuo operator new & operator delete
Is it safe for Anxin securities to open an account online?
使用easyexcel模板导出的两个坑(Map空数据列错乱和不支持嵌套对象)
Microwave radar induction module technology, real-time intelligent detection of human existence, static micro motion and static perception
Is it safe to open a mobile stock account? Is it reliable?
Reinforcement learning - learning notes 4 | actor critical
Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL
不愧是大佬,字节大牛耗时八个月又一力作
Debezium series: parsing the default value character set
How to apply smart contracts more wisely in 2022?