当前位置:网站首页>random list随机生成不重复数
random list随机生成不重复数
2022-06-24 22:55:00 【Silence丶你的名字】
有一个需求,需要随机生成六位数,从100001 ---999999 但是又要不重复,且不影响性能。如果每次生成都去判断是否重复,当生成的次数足够多,会影响性能。
我想的是开一条线程,提前去处理。 提前生成好随机的数。直接用就好了
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();
//启动线程
new Thread(codeRandom::start).start();
只取,不删,所以不涉及线程安全问题。如果删了list的话,后面生成的数可能导致重复
边栏推荐
- 保险APP适老化服务评测分析2022第06期
- Rod and Schwartz cooperated with ZhongGuanCun pan Lianyuan Institute to carry out 6G technology research and early verification
- The ecosystem of the yuan universe
- Use of hashcat
- Investigation on key threats of cloud computing applications in 2022
- ProcessOn制作ER过程(自定义)
- Jetson Nano 从入门到实战(案例:Opencv配置、人脸检测、二维码检测)
- Resolution of cross reference in IDA
- Computing service network: a systematic revolution of multi integration
- Integration of metersphere open source continuous testing platform and Alibaba cloud cloud cloud efficient Devops
猜你喜欢
It's 2022, and you still don't know what performance testing is?
疫情防控,居家办公,网上授课之心得 | 社区征文
Talking about the advantages of flying book in development work | community essay solicitation
一线城市软件测试工资——你拖后腿了吗
如何通过EasyCVR接口监测日志观察平台拉流情况?
DDD概念复杂难懂,实际落地如何设计代码实现模型?
EasyCVR国标协议接入的通道,在线通道部分播放异常是什么原因?
【直播回顾】战码先锋第七期:三方应用开发者如何为开源做贡献
What is the reason for the disconnection of video playback due to the EHOME protocol access of easycvr platform?
Pit entry machine learning: I. Introduction
随机推荐
同花顺是正规平台吗?同花顺开户安全吗
LINQ 查询(3)
Sumati gamefi ecological overview, element design in the magical world
mysql命令备份
How to quickly familiarize yourself with the code when you join a new company?
Resolution of cross reference in IDA
Smartctl opens the device and encounters permission denied problem troubleshooting process record
Basic layout -qhboxlayout class, qvboxlayout class, qgridlayout class
Multimodal emotion recognition_ Research on emotion recognition based on multimodal fusion
What are the reasons for the abnormal playback of the online channel of the channel accessed by easycvr national standard protocol?
The role of software security testing, how to find a software security testing company to issue a report?
华泰证券如何开户能做到万分之一?证券开户安全可靠吗
Uncaught Error: [About] is not a <Route> component. All component children of <Routes> must be a <Ro
Do you know your ABC
Left hand dreams right hand responsibilities GAC Honda not only pays attention to sales but also children's safety
字符串数组转换为list集合
Redis
记一次beego通过go get命令后找不到bee.exe的坑
Half of the year has passed. How many flags have been achieved at the end of the year?
入职一家新公司,如何快速熟悉代码?