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

Answer: 
Take a look here:

* <http://www.mathsisfun.com/combinatorics/combinations-permutations.html>

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

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