random内置模块

import random 

random.random() #生成0-1的随机浮点数 

random.randint(1, 10) #生成1-10的整数 

random.randrange(1,10) #生成1-9的整数 

random.choice('hello') #随机选择里面的数 ,可以是元组,列表,字符串 

random.sample('hello', 2) # 随机挑选两个数, 2 是key值

random.uniform(1, 10) # 在区间内选择浮点数 

l = [1, 2, 3, 4, 5, 6]

random.shuffle(l)

#生成验证码(带有英文和数字)

verification_code = '' 

for i in range(0,4):

     c = random.randint(0,3) 

     if i==c:

         temp = random.randint(65,90)

     else:

          temp =  random.randint(0, 9) 

      verification += str(temp)     #组合

猜你喜欢

转载自www.cnblogs.com/my-love-is-python/p/9076233.html