Skip to content

Instantly share code, notes, and snippets.

@ggdio
Created July 30, 2019 01:27
Show Gist options
  • Save ggdio/46224189921abf3a97a8a8908ad69296 to your computer and use it in GitHub Desktop.
Save ggdio/46224189921abf3a97a8a8908ad69296 to your computer and use it in GitHub Desktop.
A concurrent random UUID generator
public class ConcurrentUUIDGenerator {
public static UUID randomUUID() {
final Random rnd = ThreadLocalRandom.current();
long mostSig = rnd.nextLong();
long leastSig = rnd.nextLong();
// Identify this as a version 4 UUID, that is one based on a random value.
mostSig &= 0xffffffffffff0fffL;
mostSig |= 0x0000000000004000L;
// Set the variant identifier as specified for version 4 UUID values. The two
// high order bits of the lower word are required to be one and zero, respectively.
leastSig &= 0x3fffffffffffffffL;
leastSig |= 0x8000000000000000L;
return new UUID(mostSig, leastSig);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment