0

I'd like to generate unique random strings with Python, so I'm leveraging uuid4.

from uuid import uuid4

def generate_random_string():
    return uuid4().hex

The problem is that it generates strings with too many characters. One possible solution that I came up with is to convert the string from base 16 to base 62 (digits + lowercase/uppercase letters). It works, but the result still has too many characters (22 to be more precise). Something between 6 and 10 chars would be great, but I'm afraid they might not be unique some time in the future. Is there any way to overcome this?

3
  • That's an option, but it has a higher risk to be duplicated is the future. Commented Feb 10, 2014 at 0:53
  • 2
    No higher than any other 10-character string. If you want less chance of collision, you need more characters. That's just a fact of nature. Commented Feb 10, 2014 at 0:55
  • And the chance of collision between two 10-character case-sensitive alnum strings is less than winning the lottery (jackpot) twice in a row. Just for scale. Commented Feb 10, 2014 at 0:59

1 Answer 1

1

This gives control over the length of the generated string, however, as @Keven mentioned in the comments, if you want less chance of collision in the future, you need to use more characters. Though it seems quite unlikely.

from random import sample
from string import digits, ascii_letters 

chars = digits + ascii_letters
#'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

gen_str = sample(chars, 10)

example output:

['2', '8', 'G', 'o', 'U', 'e', 'K', 'f', 'w', '7']

>>> ''.join(gen_str)

'28GoUeKfw7'

info on random module

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.