M.J.
M.J.

Reputation: 16656

how many unique ids can be generated

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..

Upvotes: 2

Views: 5928

Answers (6)

Buhake Sindi
Buhake Sindi

Reputation: 89169

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.

Upvotes: 9

oezi
oezi

Reputation: 51807

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

in your case:

36^8 = 2821109907456

Upvotes: 1

Colin
Colin

Reputation: 551

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.

Upvotes: -1

Victor Nicollet
Victor Nicollet

Reputation: 24577

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

Upvotes: 2

icyrock.com
icyrock.com

Reputation: 28618

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

Upvotes: 2

DaWilli
DaWilli

Reputation: 4898

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

Upvotes: 2

Related Questions