当前位置:网站首页>Random list random generation of non repeating numbers
Random list random generation of non repeating numbers
2022-06-25 02:23:00 【Silence, your name】
There is a need , You need to randomly generate six digits , from 100001 ---999999 But without repeating , And it doesn't affect performance . If every time you are born, you can judge whether it is repeated , When enough times are generated , Will affect performance .
I want to open a thread , Handle it in advance . Generate random numbers in advance . Just use it directly
package cn.silence.random;
import cn.hutool.core.util.RandomUtil;
import java.util.ArrayList;
import java.util.List;
public class CodeRandom {
private final List<Integer> list = new ArrayList<>(1000000);
private int index = 0;
public void start() {
while (list.size() < 1000000) {
int code = RandomUtil.randomInt(100000, 999999);
if (!list.contains(code)) {
list.add(code);
}
}
}
public Integer getCode() {
if (index >= list.size()) {
return RandomUtil.randomInt(100000, 999999);
}
return list.get(index++);
}
}
CodeRandom codeRandom = new CodeRandom();
// Start thread
new Thread(codeRandom::start).start();


Take only , Don't delete , So thread safety is not involved . If you delete list Words , The number generated later may cause repetition
边栏推荐
- Chrysanthemum chain (winter vacation daily question 39)
- When an interface has an exception, how do you analyze the exception?
- |遇到bug怎么分析,专业总结分析来了
- yarn : 无法加载文件 C:\Users\xxx\AppData\Roaming\npm\yarn.ps1,因为在此系统上禁止运行脚本
- 【STL源码剖析】STL六大组件功能与运用(目录)
- 对进程内存的实践和思考
- 消息称一加将很快更新TWS耳塞、智能手表和手环产品线
- 内网学习笔记(5)
- 非凸联合创始人李佐凡:将量化作为自己的终身事业
- [live review] battle code pioneer phase 7: how third-party application developers contribute to open source
猜你喜欢
随机推荐
Intranet learning notes (5)
What is the reason for the disconnection of video playback due to the EHOME protocol access of easycvr platform?
Redistemplate operates redis. This article is enough (I) [easy to understand]
【Proteus仿真】Arduino UNO+继电器控制照明设备
Use of hashcat
如何卸载cuda
当他们在私域里,掌握了分寸感
tmux 如何自定义背景颜色 | How does the tmux color palette work?
内网学习笔记(7)
【STL源码剖析】STL六大组件功能与运用(目录)
如何选择正规安全的外汇交易平台?
把 Oracle 数据库从 Windows 系统迁移到 Linux Oracle Rac 集群环境(2)——将数据库转换为集群模式
会自动化—10K,能做自动化—20K,你搞懂自动化测试没有?
对进程内存的实践和思考
软件测试人员的7个等级,据说只有1%的人能做到级别7
ProcessOn制作ER过程(自定义)
保险APP适老化服务评测分析2022第06期
Qt中使用QDomDocument操作XML文件
Rod and Schwartz cooperated with ZhongGuanCun pan Lianyuan Institute to carry out 6G technology research and early verification
How do the TMUX color palette work?








