可以生成多少个唯一ID

M.J*_*.J. 1 java uuid

我使用以下代码创建一个唯一的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) {

        }
Run Code Online (Sandbox Code Playgroud)

我需要知道,如果我使用上面的代码..那么我可以生成多少独特的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"};
Run Code Online (Sandbox Code Playgroud)

请帮忙..

Buh*_*ndi 8

从本质上讲, 你可以生成36 8个字符串.

该定理通过使用离散数学(使用位串)来解释:

你有8个字符的位字符串,你需要填写36个字符中的1个:

__  __  __  __  __  __  __  __  
36  36  36  36  36  36  36  36  (characters a -- z, 0-- 9)
Run Code Online (Sandbox Code Playgroud)

因此,您有36*36*36*36*36*36*36*36= 36 8 = 2,821,109,907,456总ID.