python内置模块-random
print(random.randint(1,10)) 生成随机整数,下限必须小于上限
print(random.randrange(1,10)) 生成随机整数,参数为([start],stop,[step])
random.random() 生成0,1的随机浮点数
随机生成4位验证码:
1 import random 2 checkcode = '' 3 for i in range(4): 4 current =random.randrange(0,4) 5 if current == 2: 6 temp = chr(random.randint(65,90)) #大写字母 7 elif current != 3: 8 temp = chr(random.randint(97,122)) #小写字母 9 else: 10 temp = random.randint(0,9) 11 checkcode += str(temp) 12 print(checkcode)