河南省建设协会网站,软件开发文档包括哪些,深圳麒麟网站建设,做网站建设小程序有一个需求#xff0c;需要随机生成六位数#xff0c;从100001 ---999999但是又要不重复#xff0c;且不影响性能。如果每次生成都去判断是否重复#xff0c;当生成的次数足够多#xff0c;会影响性能。我想的是开一条线程#xff0c;提前去处理。 提前生成好随机的数。直…有一个需求需要随机生成六位数从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 ListInteger 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的话后面生成的数可能导致重复