python生成随机数(2种方式)

#应用场景:生成随机验证码

import random


checkcode= ''

for iin range(6):

    current= random.randrange(0,6)

    if current!= i:

        temp= chr(random.randint(65,90))

    else:

        temp= random.randint(0,9)

    checkcode+= str(temp)#+=和join,join效率高

print (checkcode)

#第二种方式


code=[]

for iin range(6):

  if i==random.randint(1,5):

      code.append(str(random.randint(1,5)))

  else:

      temp=chr(random.randint(65,90))

      code.append(temp)

print(''.join(code))

你可能感兴趣的:(python生成随机数(2种方式))