Quantcast
Channel: How to generate a random alpha-numeric string - Stack Overflow
Viewing all articles
Browse latest Browse all 49

Answer by Michael Allen for How to generate a random alpha-numeric string?

$
0
0

Surprising no-one here has suggested it but:

import java.util.UUID

UUID.randomUUID().toString();

Easy.

Benefit of this is UUIDs are nice and long and guaranteed to be almost impossible to collide.

Wikipedia has a good explanation of it:

" ...only after generating 1 billion UUIDs every second for the next 100 years, the probability of creating just one duplicate would be about 50%."

http://en.wikipedia.org/wiki/Universally_unique_identifier#Random_UUID_probability_of_duplicates

The first 4 bits are the version type and 2 for the variant so you get 122 bits of random. So if you want to you can truncate from the end to reduce the size of the UUID. It's not recommended but you still have loads of randomness, enough for your 500k records easy.


Viewing all articles
Browse latest Browse all 49

Trending Articles