I need to a unique and 64-bit ID Generator? I know that UUID is 128 bit but If exist any version of id generator in java get it to me. It's important that the ids are not sequence.
Asked
Active
Viewed 1,486 times
0
-
You can always use a non-repeating pseudo-random number generator. – Hot Licks May 07 '14 at 11:27
-
@HotLicks : I thought the usual interpretation of random generator period was that they didn't restart the exact same sequence, not that they didn't emit the same number? – monocell May 07 '14 at 12:53
-
@monocell - Depends on the RNG. There is no single definition that encompasses everything. – Hot Licks May 07 '14 at 15:14
2 Answers
3
The least significant half of a UUID in Java is very unique, and that would give you a 64bit number.
UUID.randomUUID().getLeastSignificantBits()
Also see: Likelihood of collision using most significant bits of a UUID in Java
As pointed out in the comments, the number is not guaranteed unique, but duplications are unlikely
-
2Not guaranteed to be unique, of course, but dupes would be "highly unlikely". – Hot Licks May 07 '14 at 15:11
-
Thanks @HotLicks, I was trying to convery that, but I think my choice on grammar was not the best :) – Leon May 07 '14 at 16:59
0
you could use a Blowfish encryption over a normal counter, or generate a UUID4 and xor the first and last 16 bytes.

Amir Arad
- 6,724
- 9
- 42
- 49