vendredi 21 octobre 2016

Generating unique ids with length of 4 digits or letters

I have this code:

private static String[] symbols = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "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" };
public static String getSequence(int i) {
    return symbols[i / (symbols.Length * symbols.Length)] 
         + symbols[(i / symbols.Length) % symbols.Length] 
         + symbols[i % symbols.Length];
}

This generates a unique sequence like so: 001 002 003 ... 009 00a 00b ... 00z 010 011 012 ... 01z etc

The problem is that I need these unique ids to have the length of 4.

How can I rewrite that method to achieve that ? I understand how it works but my problem is with [i / (symbols.Length * symbols.Length)] because any number bigger than 46656 will throw index out of range.

Or is there a better method for generating unique ids with length of 4 ? (only letters and digits).




Aucun commentaire:

Enregistrer un commentaire