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

9

38

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();
Wipe answered 11/11, 2011 at 10:57 Comment(0)
M
49

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

Manifestation answered 18/7, 2016 at 14:6 Comment(3)
How to maintain insertion order using UUID with numbers?Maddi
@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...Manifestation
How to specify the length for it?Arbiter
D
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).

Dorison answered 26/5, 2018 at 13:49 Comment(0)
D
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.

Disservice answered 11/11, 2011 at 10:58 Comment(1)
Did I fail to mention it ? @pheonixDisservice
L
6

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.

Lipfert answered 30/1, 2017 at 10:52 Comment(2)
A UUID is just a sufficiently long random number that a collision is astronomically unlikely.Oddson
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.Allargando
S
6
UUID uuid = UUID.randomUUID();
String numericUUID = Long.toString(uuid.getMostSignificantBits())
                   + Long.toString(uuid.getLeastSignificantBits());
Senatorial answered 9/2, 2018 at 19:43 Comment(2)
How can we specify the length for it?Arbiter
I think the length is fixed for UUIDs.Senatorial
S
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.

Silvasilvain answered 11/11, 2011 at 11:1 Comment(1)
That will not ensure uniqueness. This is the primary requirement for IDs.Hateful
H
0

I was looking for a solution which gives an almost unique id but not of as massive size as bigint-ing UUID which I think gives around 40 digit numbers.

What I did was that I bigint-ed this combination of epoch timestamp in milliseconds and UUID -

// javascript

const currTimestampInMillis = Date.now().toString();
const uuid = xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx; // generated UUID
const id = BigInt('0x' + currTimestampInMillis + uuid.split('-')[0]);

id generated here is a 25 digit almost unique id. It is extremely unlikely that two combinations of current timestamp in milliseconds and first 8 characters of UUID will collide.

Plus the added advantage is that all IDs will be sorted so this helps in database indexing.

Hateful answered 31/5, 2024 at 9:53 Comment(0)
T
-4

The simpliest way:

uuid.uuid4().int
Tamratamsky answered 13/12, 2019 at 20:11 Comment(1)
This answer would be more helpful if it explained why this worked, instead of just providing the code :)Moorer
F
-5

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

Fewer answered 11/11, 2011 at 11:0 Comment(3)
hashCode() is not an invertible function. Should never be used for uniqueness.Senatorial
A poorly performing but perfectly correct implementation of hashCode() would be return 1;Oddson
I'm not sure why this ended up in the VLQ queue, because it's really not. It's a legitimate attempt to answer the question (even if it's poorly performing). Wrong answers are not Very Low Quality.Cornet

© 2022 - 2025 — McMap. All rights reserved.