>>> import random
random.random()#随机生成浮点数,范围是[0,1),大于0,小于1

random.randint()#随机生成整数,范围是[a,b]

random.randrange(2)#随机生成整数,范围是[0,2)

random.choice('hello')#随机选择序列(字符串,元组,列表)里面的值

>>> random.sample('hello',2) #从前面的序列当中随机取2位返回一个列表
['h', 'l']

random.uniform(1,3)#返回的是1-3之间的浮点数,大于1,小于3

>>> x = [1,2,3,4,5,6]
>>> random.shuffle(x)#洗牌的功能,把有顺序打乱成无序的
>>> x
[2, 5, 6, 3, 4, 1]

 

import random
checkcode = ''
for i in range(4):
res = random.randint(1,4)
if res == i :
tmp = chr(random.randint(65,90))
else:
tmp = random.randint(0, 9)
checkcode += str( tmp )
print(checkcode)
posted on 2018-05-30 17:57  Linux的爱好者  阅读(624)  评论(0编辑  收藏  举报