I need to generate a unique long value, so I decided to use UUID:
UUID.randomUUID().getLeastSignificantBits();
One strange thing I noticed was that UUID.randomUUID().getLeastSignificantBits()
always returned negative values. I am confused. Am I missing something?
Wikipedia says:
Version 4 UUIDs use a scheme relying only on random numbers. This algorithm sets the version number (4 bits) as well as two reserved bits. All other bits (the remaining 122 bits) are set using a random or pseudorandom data source.
Version 4 UUIDs have the form xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx where x is any hexadecimal digit and y is one of 8, 9, A, or B
So the first bit of the least significant half will always be a 1, making it a negative number.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With