可以生成多少个唯一的 id

发布于 2024-10-04 13:46:11 字数 1613 浏览 0 评论 0原文

我使用下面的代码创建一个唯一的 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"};

请帮忙..

i am using the below code to create a unique Id which is 8 character (including numeric and alphanumeric characters).

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) {

        }

i need to know, if i use the above code.. then how many unique ids i can generate. Given

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"};

Please help..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

风情万种。 2024-10-11 13:46:11

本质上,您总共可以生成 368 个字符串。

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

您有 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= ID 总数为 2,821,109,907,456

In essence there is a 368 total strings you can generate.

This theorem is explained by using Discrete Mathematics (using Bit String):

You have 8 character bit string and you need to fill choose 1 out of 36 characters:

__  __  __  __  __  __  __  __  
36  36  36  36  36  36  36  36  (characters a -- z, 0-- 9)

Therefore you have 36*36*36*36*36*36*36*36 = 368= 2,821,109,907,456 total id's.

夜声 2024-10-11 13:46:11

(26+10)^8 = 2 821 109 907 456

(26+10)^8 = 2 821 109 907 456

撩起发的微风 2024-10-11 13:46:11

看看这里:

部分重复排列。理论上,给定一组 n 元素(即 bitsArray 的长度)和排列的长度(即您的 uuidString 字符串) r,您将能够生成 n^r 个独特的排列(即您的情况下的 UUID)。

在你的例子中,n = 36(26个字母和10个数字)和r = 8(uuid的长度为8),所以它是:

36^8 = 2821109907456

Take a look here:

Section Permutations with Repetition. In theory, given a set of n elements (i.e. length of your bitsArray) and the length of the permutation (i.e. your uuidString string) being r, you will be able to generate n^r unique permutations (i.e. UUIDs in your case).

In your case, n = 36 (26 letters and 10 numbers) and r = 8 (length of uuid is 8), so it's:

36^8 = 2821109907456
最好是你 2024-10-11 13:46:11

有多少个唯一 ID?大约一百万个。这取决于您使用的随机数生成器的质量。如果该随机数生成器始终返回 4,则您只能生成一个标识符。如果它是低质量的线性同余生成器(低位的随机性非常差),您最终得到的值可能比理论最大值(即 368 = 大约 28000 亿)少 256 倍。然而,由于您生成的每个 ID 都需要读取之前生成的 ID 的整个列表,因此我怀疑您的计算机在达到 100 万个标识符之前就会摆脱困境。

有多少个 UUID? 零。您的代码依赖于内部黑名单来避免冲突,这意味着使用您的方法生成 UUID 的多台计算机有相当合理的机会最终发生冲突(避免冲突是首先使用 UUID 的全部目的)。

How many unique IDs? About a million. It depends on the quality of the random number generator you are using. If that random number generator always returns 4, you can only generate one identifier. If it's a low-quality linear congruential generator (that has pretty bad randomness for the lower bits) you could end up with 256 times fewer values than the theoretical maximum (which is 368 = about 2800 billion). However, since every ID you generate needs to read the entire list of previously generated IDs, I suspect your computer will just put itself out of its misery before it reaches one million identifiers.

How many UUIDs? Zero. Your code relies on an internal black list to avoid collisions, which means several computers generating UUIDs with your method have a fairly reasonable chance of ending up with a collision (avoiding that collision was the entire point of using UUIDs in the first place).

夏雨凉 2024-10-11 13:46:11

[可能的字符数]^[生成 ID 的长度] -

在您的情况下非常简单的数学:

36^8 = 2821109907456

[number of possible char]^[lenght of the generatet id] - very simple math

in your case:

36^8 = 2821109907456

倾`听者〃 2024-10-11 13:46:11

数组的长度是 36。并且您使用方法r.nextInt(length)
这意味着随机数的最大值是36,从0到35。所以最多可以得到8个索引36。

the length of the array is 36. And you use the method r.nextInt(length)
it means that the max value of the random number is 36,from 0 to 35. so at most you can get 8 index 36.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文