python>random随机数模块

xiaoxiao2025-11-25  8

random.random()  #生成一个0-1的随机小数

random.randint(1,8)  #生成一个1-8的随机数(包括右边的)

random.choice([1,2,[1,88],[5,6]])  #列表随机数

random.sample([1,2,5,8,6,89,4,5,6,8],6)  #从一个序列中取出n个数

random.randrange(1,9)  #生成随机数不包括右边的

随机生成5位的数字字母验证码

def V_code():

  code = ''

  for i in range(5):

    add = random.choice([random.randrange(10),chr(random.randrange(65,91))])

    code += str(add)

  print(code)

V_code()

转载请注明原文地址: https://www.6miu.com/read-5040014.html

最新回复(0)