我正在使用下面的代码创建一个唯一的 Id,它是 8 个字符(包括数字和字母数字字符)。
try {
List<String> uuidList = new ArrayList<String>();
int counter = 1;
File file = new File("D://temp//temp1.txt");
file.createNewFile();
Writer writer = new FileWriter(file);
BufferedWriter wr = new BufferedWriter(writer);
while(true) {
int length = bitsArray.length;
Random r = new Random();
StringBuffer uuid = new StringBuffer();
for(int i= 0; i < 8; i++) {
int nextRandomId = r.nextInt(length);
uuid.append(bitsArray[nextRandomId]);
}
String uuidString = uuid.toString();
wr.write(uuidString);
wr.newLine();
if(counter != 1 && uuidList.contains(uuidString)) {
Thread.sleep(1000);
System.err.println(counter);
break;
}
//061e735145fc
System.err.println(uuidString);
uuidList.add(uuidString);
counter++;
}
} catch (Exception e) {
}
我需要知道,如果我使用上面的代码.. 那么我可以生成多少个唯一 ID。给定
static String[] bitsArray = {"a","b","c","d","e","f","g","h","i",
"j","k","l","m","n","o","p","q","r",
"s","t","u","v","w","x","y","z",
"0","1","2","3","4","5","6","7","8","9"};
请帮忙..
最佳答案
本质上,您可以生成的字符串总数为 368。
这个定理用Discrete Mathematics(使用Bit String)来解释:
你有 8 个字符的位串,你需要从 36 个字符中选择 1 个来填充:
__ __ __ __ __ __ __ __
36 36 36 36 36 36 36 36 (characters a -- z, 0-- 9)
因此你有 36*36*36*36*36*36*36*36
= 368= 2,821,109,907,456
总 ID。
关于java - 可以生成多少个唯一 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4278191/