random 模块

>>> import random
#随机小数
>>> random.random()      # 大于0且小于1之间的小数
0.7664338663654585
>>> random.uniform(1,3) #大于1小于3的小数
1.6270147180533838




random用来生成随机数
#随机整数 >>> random.randint(1,5) # 大于等于1且小于等于5之间的整数 >>> random.randrange(1,10,2) # 大于等于1且小于10之间的奇数 #从序列中随机选择一个返回 >>> random.choice([1,'23',[4,5]]) # #1或者23或者[4,5] #随机选择多个返回,返回的个数为函数的第二个参数 >>> random.sample([1,'23',[4,5]],2) # #列表元素任意2个组合,不会有重复的 [[4, 5], '23'] #打乱列表顺序 >>> item=[1,3,5,7,9] >>> random.shuffle(item) # 打乱次序 >>> item [5, 1, 3, 7, 9] >>> random.shuffle(item) >>> item [5, 9, 7, 1, 3]

生成随机数:包含数字大小写字母

方法一:

list=["a","b","c","d","e","1","2","3","4","5","6","7","8","9","0"] 把所有情况都列出了
password=[]
for i in range(6):
    ret=random.choice(list)
    password.append(ret)
password="".join(password)
print(password)

方法二:可以生成n个随机数大小写字母加数字

list=[]
def func(time):
    for i in range(time):
        ret1 = chr(random.randint(65, 90))
        ret3 =chr(random.randint(97, 122))
        num = random.randint(0, 9)
        ret2 = random.choice([ret1, str(num),ret3])
        list.append(ret2)
    list1="".join(list)
    print(list1)
func(7)
原文地址:https://www.cnblogs.com/sticker0726/p/7840363.html