Is there a way to generate a random UUID, which consists only of numbers?
Solution 1
If you dont want a random number, but an UUID with numbers only use:
String lUUID = String.format("%040d", new BigInteger(UUID.randomUUID().toString().replace("-", ""), 16));
in this case left padded to 40 zeros...
results for:
UUID : b55081fa-9cd1-48c2-95d4-efe2db322a54
in:
UUID : 0241008287272164729465721528295504357972
Solution 2
For the record: UUIDs are in fact 128 bit numbers.
What you see as an alphanumeric string is the representation of that 128 bit number using hexadecimal digits (0..9A..F).
The real solution is to transform the string into it's correspondent 128 bit number. And to store that you'll need two Longs (Long has 64 bit).
Solution 3
Why don't just generate Random
number and put it into format you want?
This won't give you uniqueness out of the box. (i.e. You will have to implement check on each generation and retry logic)
Where as other solutions which are taking UUID bits and converting them to number will be more granular in uniqueness. depending on your usecase you might still want to check uniqueness with this approach.
Solution 4
Although the original poster of this question marked @Jigar Joshi's question as the best answer, I'd like to add that there is a rationale to use UUIDs rather than random numbers in certain cases. The difference is that UUIDs are (universally) unique (hence the name) while random numbers are not. It's just a question of probability whether you get the same number twice or even more often when generating random numbers, but this will never happen with UUIDs. Therefore, I'd consider @Chris Eibl's answer as the best one.
Solution 5
UUID uuid = UUID.randomUUID();
String numericUUID = Long.toString(uuid.getMostSignificantBits())
+ Long.toString(uuid.getLeastSignificantBits());
Related videos on Youtube
rosh
Updated on December 25, 2020Comments
-
rosh over 3 years
Java's UUID class generates a random UUID. But this consists of letters and numbers. For some applications we need only numbers. Is there a way to generate random UUID that consists of only numbers in Java?
UUID.randomUUID();
-
james.garriss over 10 yearsUUIDs include letters.
-
Jonathan Schneider over 6 years
hashCode()
is not an invertible function. Should never be used for uniqueness. -
Kevin Krumwiede almost 6 yearsA poorly performing but perfectly correct implementation of
hashCode()
would bereturn 1;
-
Kevin Krumwiede almost 6 yearsA UUID is just a sufficiently long random number that a collision is astronomically unlikely.
-
jmj almost 6 yearsDid I fail to mention it ? @pheonix
-
MyStackRunnethOver over 4 yearsThis answer would be more helpful if it explained why this worked, instead of just providing the code :)
-
Admin over 4 yearsThey used to be actually unique -- there was a spacewise component (typically equal to the globally-unique MAC of an interface you had) and a timewise component (from the clock of the machine with that interface). So as long as (a) your average UUID-generation frequency was longer than your clock update interval, and (b) your timers were sync'd closer than the time it took you to unplug the interface from one backplane and insert it into a different computer, and (c) you had builtin provision for handling time going backwards, they were indeed unique.
-
Panup Pong about 4 yearsHow to maintain insertion order using UUID with numbers?
-
Chris Eibl about 4 years@PanupongKongarn please elaborate? Do you talk about databases? If yes, then you can use an additional column (auto-incremented or a sequence) in your db of choice...
-
Arslan over 2 yearsHow to specify the length for it?
-
Arslan over 2 yearsHow can we specify the length for it?
-
Jonathan Schneider over 2 yearsI think the length is fixed for UUIDs.