当前位置:网站首页>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的话,后面生成的数可能导致重复
边栏推荐
- It's 2022, and you still don't know what performance testing is?
- 实战攻防演练中的四大特点
- qt打包exe文件,解决“无法定位程序输入点_ZdaPvj于动态链接库Qt5Cored.dll”
- Test / development programmers, 30, do you feel confused? And where to go
- 2022-06-24:golang选择题,以下golang代码输出什么?A:1;B:3;C:4;D:编译失败。 package main import ( “f
- 【STL源码剖析】STL六大组件功能与运用(目录)
- [live review] battle code pioneer phase 7: how third-party application developers contribute to open source
- Specific list of regular and safe domestic stock trading account opening
- 如何选择正规安全的外汇交易平台?
- Can automate - 10k, can automate - 20K, do you understand automated testing?
猜你喜欢

都2022年了,你还不了解什么是性能测试?

入职一家新公司,如何快速熟悉代码?

Lizuofan, co-founder of nonconvex: Taking quantification as his lifelong career

Sumati gamefi ecological overview, element design in the magical world

Is it out of reach to enter Ali as a tester? Here may be the answer you want

DDD concept is complex and difficult to understand. How to design code implementation model in practice?

Sumati GameFi生态纵览,神奇世界中的元素设计

EasyCVR平台EHOME协议接入,视频播放出现断流是什么原因?
![[live review] battle code pioneer phase 7: how third-party application developers contribute to open source](/img/ad/26a302ca724177e37fe123f8b75e4e.png)
[live review] battle code pioneer phase 7: how third-party application developers contribute to open source

It's 2022, and you still don't know what performance testing is?
随机推荐
同花顺是正规平台吗?同花顺开户安全吗
DataEase模板市场正式发布
Are programmers from Huawei, Alibaba and other large manufacturers really easy to find?
mysql命令备份
tmux 如何自定义背景颜色 | How does the tmux color palette work?
3 years of testing experience. I don't even understand what I really need on my resume. I need 20K to open my mouth?
Is the compass reliable? Is it safe to open a securities account?
EasyCVR国标协议接入的通道,在线通道部分播放异常是什么原因?
2022年云计算应用关键威胁调查
字符串数组转换为list集合
【STL源码剖析】配置器(待补充)
EasyCVR平台EHOME协议接入,视频播放出现断流是什么原因?
实战攻防演练中的四大特点
Android物联网应用程序开发(智慧园区)—— 设置传感器阈值对话框界面
Redis 那些事
Kaggle 专利匹配比赛赛后总结
Taishan Office Technology Lecture: a simple study of Chinese punctuation in vertical arrangement
Half of the year has passed. How many flags have been achieved at the end of the year?
进入阿里做测试员遥不可及?这里或许有你想要的答案
Do you know your ABC