当前位置:网站首页>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的话,后面生成的数可能导致重复
边栏推荐
- DDD concept is complex and difficult to understand. How to design code implementation model in practice?
- Talking about the advantages of flying book in development work | community essay solicitation
- Stocking but not completely stocking (daily question 2 in spring)
- Intranet learning notes (5)
- Left hand dreams right hand responsibilities GAC Honda not only pays attention to sales but also children's safety
- jwt
- 【Proteus仿真】Arduino UNO+数码管显示4x4键盘矩阵按键
- psql 列转行
- 华泰证券如何开户能做到万分之一?证券开户安全可靠吗
- 2个NPN三极管组成的恒流电路
猜你喜欢

3 years of testing experience. I don't even understand what I really need on my resume. I need 20K to open my mouth?

Intégration de la plate - forme de test continu open source de metersphere avec Alibaba Cloud Effect devops

DataEase模板市场正式发布

Hashcat 的使用

Is it out of reach to enter Ali as a tester? Here may be the answer you want
Cusdis - 轻量级、隐私优先的开源评论系统 | 倾城之链

EasyCVR平台EHOME协议接入,视频播放出现断流是什么原因?

谈谈飞书对开发工作的优势 | 社区征文

测试/开发程序员,30而立,你是否觉得迷茫?又当何去何从......

Sumati gamefi ecological overview, element design in the magical world
随机推荐
入坑机器学习:一,绪论
File system - basic knowledge of disk and detailed introduction to FAT32 file system
Sumati GameFi生态纵览,神奇世界中的元素设计
探索C语言程序奥秘——C语言程序编译与预处理
ida中交叉引用的解析
同花顺是正规平台吗?同花顺开户安全吗
[mobile terminal] design size of mobile phone interface
都2022年了,你还不了解什么是性能测试?
Test / development programmers, 30, do you feel confused? And where to go
Integration of metersphere open source continuous testing platform and Alibaba cloud cloud cloud efficient Devops
EasyCVR国标协议接入的通道,在线通道部分播放异常是什么原因?
How to get the picture outside the chain - Netease photo album [easy to understand]
Post competition summary of kaggle patent matching competition
[I.MX6UL] U-Boot移植(六) 网络驱动修改 LAN8720A
js正则匹配数字、大小写字母、下划线、中线和点[通俗易懂]
write a number of lines to a new file in vim
Of the seven levels of software testers, it is said that only 1% can achieve level 7
internship:svn的使用
qt打包exe文件,解决“无法定位程序输入点_ZdaPvj于动态链接库Qt5Cored.dll”
2022-06-24:golang选择题,以下golang代码输出什么?A:1;B:3;C:4;D:编译失败。 package main import ( “f