lost and found ( for me ? )

Python : How to generate random string

You can generate random characters using python radom module.
# python
Python 2.4.3 (#1, May  5 2011, 16:39:10)
>>> import random
>>> import string
>>> char_set = string.ascii_lowercase + string.ascii_uppercase + string.digits
>>> print ''.join(random.choice(char_set) for x in range(40))
FUsauzThVWHHggbrPEZgkzts3spkJl8TmpIx9t0P


Here’s a sample python script of generating random string. (generate_random_string.py)
#!/bin/env python

import random,string,sys

char_set = string.ascii_lowercase + string.ascii_uppercase + string.digits
s = ''.join(random.choice(char_set) for x in range (int(sys.argv[1])))
print s


generate 40 random characters.
# ./generate_random_string.py 40
VeIP1C3aPmhgwf0uz6UxZwPGsvrGEcaxCzEWhkds

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.