Efficiently generate a 16-character, alphanumeric string

Posted by ensnare on Stack Overflow See other posts from Stack Overflow or by ensnare
Published on 2010-03-24T20:40:16Z Indexed on 2010/03/24 20:43 UTC
Read the original article Hit count: 240

I'm looking for a very quick way to generate an alphanumeric unique id for a primary key in a table.

Would something like this work?

def genKey():
    hash = hashlib.md5(RANDOM_NUMBER).digest().encode("base64")
    alnum_hash = re.sub(r'[^a-zA-Z0-9]', "", hash)
    return alnum_hash[:16]

What would be a good way to generate random numbers? If I base it on microtime, I have to account for the possibility of several calls of genKey() at the same time from different instances.

Or is there a better way to do all this?

Thanks.

© Stack Overflow or respective owner

Related posts about python

Related posts about beginner