Well, I was looking for ways to generate UIDs in java code (most of them are also suitable for stackoverflow). It is best to use the java UUID to create unique identifiers as it uses a timestamp. But my problem is that it is 128-bit, and I need a shorter string, for example, 14 or 15 characters. So, I developed the following code for this.
Date date = new Date(); Long luid = (Long) date.getTime(); String suid = luid.toString(); System.out.println(suid+": "+suid.length() + " characters"); Random rn = new Random(); Integer long1 = rn.nextInt(9); Integer long2 = rn.nextInt(13); String newstr = suid.substring(0, long2) + " " + long1 + " " + suid.subtring(long2); System.out.println("New string in spaced format: "+newstr); System.out.println("New string in proper format: "+newstr.replaceAll(" ", ""));
Please note that I am just showing a line with a formatted and correctly formatted one to compare only with the original line.
Will it guarantee a 100% unique identifier every time? Or do you see any possibility of repeating numbers? Also, instead of inserting a random number into a random position that βcouldβ create repeated numbers, I could do this either at the beginning or at the end. This is necessary to fill in the required length of my UID. Although, perhaps this will not work if you need a UID of less than 13 characters.
Any thoughts?
rishi source share