import random print(random.random()) #不接收任何参数 print(random.randint(5,9)) #接收参数范围包含边界值 # random.randrange(10) print(random.randrange(10,15)) #可接受一个和两个 print(random.choice("abcd")) #可以接受一个参数,字符串也可以放列表字典等 print(random.choice(["abc",[1,2,3]])) # print(random.choices(["fefewf"])) print(random.sample(["123",4,[123],2],2)) #可以从里面随机选几个,最后一个数字是选择的个数 print(random.choice(["a","b","d","e",1,2,3,4,5]))
#下面两种方式实现5位数的随机数
def ff(): verification_code = "" for i in range(6): randomdigst = str(random.choice([chr(random.randrange(65,91)),random.randrange(1,9)])) verification_code += randomdigst print(verification_code) ff()
def ff(): verification_code = "" for i in range(6): if i == random.randrange(0,3): randomdigst = str(random.randrange(1,9)) else: randomdigst = chr(random.randrange(65,91)).lower() verification_code += randomdigst print(verification_code) ff()