Generating random strings with few characters
Question: I'd like to generate unique random strings with Python, so I'm leveraging
[uuid4](http://docs.python.org/2/library/uuid.html#uuid.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?


Answer: 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](http://docs.python.org/2/library/random.html)**

