31

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();

9 Answers 9

42

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

3
  • How to maintain insertion order using UUID with numbers?
    – Panup Pong
    Mar 13, 2020 at 7:00
  • @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...
    – Chris Eibl
    Mar 15, 2020 at 10:22
  • How to specify the length for it?
    – Arslan
    Sep 30, 2021 at 7:02
11

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).

0
6

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.

1
  • 1
    Did I fail to mention it ? @pheonix
    – jmj
    Jun 30, 2018 at 19:09
5

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.

2
  • A UUID is just a sufficiently long random number that a collision is astronomically unlikely. Jun 7, 2018 at 19:11
  • 1
    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.
    – user10762593
    Dec 14, 2019 at 0:25
4
UUID uuid = UUID.randomUUID();
String numericUUID = Long.toString(uuid.getMostSignificantBits())
                   + Long.toString(uuid.getLeastSignificantBits());
2
  • How can we specify the length for it?
    – Arslan
    Sep 30, 2021 at 7:02
  • I think the length is fixed for UUIDs. Oct 8, 2021 at 18:50
3

Why using the UUID class if you dont need an UUID? It sounds more like you need just a random number which can be achieved by using the java.util.Random class.

-4

The simpliest way:

uuid.uuid4().int
1
  • 3
    This answer would be more helpful if it explained why this worked, instead of just providing the code :) Dec 13, 2019 at 21:52
-5

Call hashCode() on the UUID to get a unique integer.

2
  • hashCode() is not an invertible function. Should never be used for uniqueness. Feb 9, 2018 at 19:40
  • A poorly performing but perfectly correct implementation of hashCode() would be return 1; Jun 7, 2018 at 19:06
-8

String uuid = UUID.randomUUID().toString().replaceAll("-", ""); //strips '-'

1
  • 3
    UUIDs include letters. Nov 23, 2013 at 16:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.