Created
March 14, 2017 02:42
-
-
Save borgle/2bf8a66499fa18b4e528b7e55833f12d to your computer and use it in GitHub Desktop.
Python 随机大集合,要善于利用 string 里面的常量(ascii_letters, ascii_lowercase, ascii_uppercase, letters, digits, hexdigits, octdigits),减少代码量。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random, string | |
# number | |
number = random.randint(1, 9) | |
# char | |
char = chr(random.randint(65, 90)) | |
char = random.choice(string.ascii_letters) | |
# string | |
length = random.randint(1, 9) | |
str = ''.join([random.choice(string.ascii_letters) for i in range(length)]) | |
str = ''.join(random.sample(string.ascii_letters, length)) | |
str = ''.join([(string.ascii_letters+string.digits)[x] for x in random.sample(range(0, 62), length)]) | |
str = ''.join(random.sample(string.ascii_letters+string.digits, length)) | |
str = ''.join(map(lambda xx:(hex(ord(xx))[2:]),os.urandom(16))) # length = 32 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment