Python 之random随机数模块

一、常用函数

random.randrange(1,100) 返回[1,100)范围内的随机数整数,不包括100
random.randint(1,100) 返回[1,100]范围内的随机数整数
random.random() 返回[0, 1)范围内随机浮点数,不包括1
random.choice("abcde") 在给定容器中随机选择一个元素
random.sample("abcde",2) 在给定容器中选择特定元素
random.shuffle(l) 随机打乱传入的容器(容器必须是可变对象)

二、生成随机码

import random
random.seed(0x1010)  #设置随机种子数
    #设置种子选择空间
s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*"
ls = [] #存取密码的列表
FirstPsw = "" #存取第一个密码的字符
 
while len(ls)<20:  #十个随机密码
    pwd = ""
    for i in range(10):
        pwd += s[random.randint(0,len(s)-1)]
    if pwd[0] in FirstPsw:
        continue
    else:
        ls.append(pwd)
        FirstPsw +=pwd[0]

猜你喜欢

转载自www.cnblogs.com/yang-2018/p/12768278.html