I am trying to generate a unique identifier of a fixed length such as the IDs that are generated by Megaupload for the uploaded files.
For example:
- ALGYTAB5
- BCLD23A6
In this example using from A-Z and 0-9 and with a fixed length of 8 the total different combinations are 2,821,109,907,456.
What if one of the generated id is already taken. Those ids are going to be stored in a database and it shouldn’t be used more than once.
How can I achieve that in Java?
Thank you.
Hmm… You could imitate a smaller
GUIDthe following way. Let first 4 bytes of your string be the encoded current time – seconds passed after Unix. And the last 4 just a random combination. In this case the only way two ID’s would coincide is that they were built at the same second. And the chances of that would be very veeery low because of the other 4 random characters.Pseudocode: