当前位置:网站首页>JVMRandom不可设置种子|问题追溯|源码追溯
JVMRandom不可设置种子|问题追溯|源码追溯
2022-07-05 19:46:00 【步尔斯特】
上一篇文章,我们介绍了几种随机数的生成策略。
那么接下来,我们来分析一下JVMRandom存在的问题。
当我们如下正常构建一个随机数的时候,是没有任何问题的。
JVMRandom jvmRandom = new JVMRandom();
int i = jvmRandom.nextInt(10);
System.out.println(i);
既然JVMRandom底层调用的是Random的构造函数,那么是否也是可以设置seed值呢?
我们来验证一下
JVMRandom jvmRandom = new JVMRandom();
jvmRandom.setSeed(2);
int i = jvmRandom.nextInt(10);
System.out.println(i);
唰~报错了
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)
让我们看一看源码,一探究竟。

JVMRandom继承了Random,并重写了setSeed方法。
并且已经不支持设置种子值。
源码如下,当constructed值为false时,就会抛出异常。
/** * Unsupported in 2.0. * * @param seed ignored * @throws UnsupportedOperationException */
public synchronized void setSeed(long seed) {
if (this.constructed) {
throw new UnsupportedOperationException();
}
}
追溯一下constructed值,默认是false
/** * Ensures that only the parent constructor can call reseed. */
private boolean constructed = false;
但是,我们观察其结构可以发现,它只有一个无参构造函数。

看看是什么样子的
/** * Constructs a new instance. */
public JVMRandom() {
this.constructed = true;
}
哦,那么没办法了,也就是说,如果我们想使用JVMRandom来构建随机数,就一定会通过无参构造函数创建对象,那么constructed就一定会是true,所以,如果你调用了setSeed方法,那么就一定会抛出上述异常。
边栏推荐
- Django使用mysqlclient服务连接并写入数据库的操作过程
- [hard core dry goods] which company is better in data analysis? Choose pandas or SQL
- 常用运算符与运算符优先级
- 软件测试是干什么的?学习有啥要求?
- MySql中的longtext字段的返回问题及解决
- Password reset of MariaDB root user and ordinary user
- C application interface development foundation - form control (6) - menu bar, toolbar and status bar controls
- [untitled]
- acm入门day1
- 【obs】QString的UTF-8中文转换到blog打印 UTF-8 char*
猜你喜欢

The relationship between temperature measurement and imaging accuracy of ifd-x micro infrared imager (module)

Webuploader file upload drag upload progress monitoring type control upload result monitoring control

软件测试是干什么的?学习有啥要求?

Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL

third-party dynamic library (libcudnn.so) that Paddle depends on is not configured correctl

强化学习-学习笔记4 | Actor-Critic

面试官:Redis中集合数据类型的内部实现方式是什么?

建议收藏,我的腾讯Android面试经历分享

Microwave radar induction module technology, real-time intelligent detection of human existence, static micro motion and static perception

MMO項目學習一:預熱
随机推荐
挖财钱堂教育靠谱安全吗?
城链科技数字化创新战略峰会圆满召开
Summer Challenge database Xueba notes, quick review of exams / interviews~
Relationship between floating elements and parent and brother boxes
Necessary skills for interview in large factories, 2022android will not die, I will not fall
众昂矿业:2022年全球萤石行业市场供给现状分析
国信证券在网上开户安全吗?
PHP利用ueditor实现上传图片添加水印
建议收藏,我的腾讯Android面试经历分享
Do you know several assertion methods commonly used by JMeter?
函数的概念及语法
Concept and syntax of function
多分支结构
信息/数据
-v parameter of GST launch
Force buckle 729 My schedule I
Is it safe for Anxin securities to open an account online?
That's awesome. It's enough to read this article
【obs】libobs-winrt :CreateDispatcherQueueController
浮动元素与父级、兄弟盒子的关系