/**
* @description: 生成10个字符的唯一随机字符串,由数字大小写字母组成
* @date 15:58 2023/10/13
* @param increment 唯一数字
* @param len 唯一随机字符长度
**/privatestaticStringgenerateUkStr(long increment,int len){char[] incrementArr =String.format("%0"+ len +"d", increment).toCharArray();Pair<Integer,Integer> pairA =Pair.of(17,33);Pair<Integer,Integer> pairB =Pair.of(49,65);StringBuilder result =newStringBuilder(len);char s;for(char c : incrementArr){int randomNum =ThreadLocalRandom.current().nextInt(0,3);if(randomNum ==0){
s = c;}elseif(randomNum ==1){
s =(char)(c +ThreadLocalRandom.current().nextInt(pairA.getKey(), pairA.getValue()+1));}else{
s =(char)(c +ThreadLocalRandom.current().nextInt(pairB.getKey(), pairB.getValue()+1));}
result.append(s);}return result.toString();}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
测试
基于自增数字生成100万个
publicstaticvoidmain(String[] args){//测试生成100万个唯一随机字符,长度为10Set<String> set =newHashSet<>();//唯一数字长度为8for(long i =11111111; i <12111111; i++){String result =generateUkStr(i,10);System.out.println(result);if(set.contains(result)){thrownewIllegalCallerException("已存在");}
set.add(result);}System.out.println(String.format("总数量%d", set.size()));}