I would like an efficient utility to generate unique sequences of bytes. UUID is a good candidate but UUID.randomUUID().toString()
generates stuff like 44e128a5-ac7a-4c9a-be4c-224b6bf81b20
which is good, but I would prefer dash-less string.
I'm looking for an efficient way to generate a random strings, only from alphanumeric characters (no dashes or any other special symbols).
So, removing the dashes won't affect the uniqueness of the UUID. However, it may cause issues with libraries that expect the dashes as part of a UUID to validate it as a UUID.
The randomUUID() method is used to retrieve a type 4 (pseudo randomly generated) UUID. The UUID is generated using a cryptographically strong pseudo random number generator.
You can use base64 encoding and reduce it to 22 characters. If you use base94 you can get it does to 20 characters. If you use the whole range of valid chars fro \u0000 to \ufffd you can reduce it to just 9 characters or 17 bytes.
This does it:
public static void main(String[] args) { final String uuid = UUID.randomUUID().toString().replace("-", ""); System.out.println("uuid = " + uuid); }
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