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();
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();
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
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).
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.
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.
UUID uuid = UUID.randomUUID();
String numericUUID = Long.toString(uuid.getMostSignificantBits())
+ Long.toString(uuid.getLeastSignificantBits());
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.
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.
The simpliest way:
uuid.uuid4().int
Call hashCode()
on the UUID to get a unique integer.
hashCode()
is not an invertible function. Should never be used for uniqueness. –
Senatorial hashCode()
would be return 1;
–
Oddson © 2022 - 2025 — McMap. All rights reserved.