Is there a way to generate a random UUID, which consists only of numbers?

35,465

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());
Share:
35,465

Related videos on Youtube

rosh
Author by

rosh

Updated on December 25, 2020

Comments

  • rosh
    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
    james.garriss over 10 years
    UUIDs include letters.
  • Jonathan Schneider
    Jonathan Schneider over 6 years
    hashCode() is not an invertible function. Should never be used for uniqueness.
  • Kevin Krumwiede
    Kevin Krumwiede almost 6 years
    A poorly performing but perfectly correct implementation of hashCode() would be return 1;
  • Kevin Krumwiede
    Kevin Krumwiede almost 6 years
    A UUID is just a sufficiently long random number that a collision is astronomically unlikely.
  • jmj
    jmj almost 6 years
    Did I fail to mention it ? @pheonix
  • MyStackRunnethOver
    MyStackRunnethOver over 4 years
    This answer would be more helpful if it explained why this worked, instead of just providing the code :)
  • Admin
    Admin over 4 years
    They 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
    Panup Pong about 4 years
    How to maintain insertion order using UUID with numbers?
  • Chris Eibl
    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
    Arslan over 2 years
    How to specify the length for it?
  • Arslan
    Arslan over 2 years
    How can we specify the length for it?
  • Jonathan Schneider
    Jonathan Schneider over 2 years
    I think the length is fixed for UUIDs.