标签:大致 nbsp rand def shuffle 验证码 random模块 class div
大致有以下几个函数:
print(random.random()) #0到1的浮点型 print(random.randint(1,6)) #1到6的整型 print(random.randrange(1,6)) #1到6的整型,不取6 print(random.choice([1,4,7,2,‘33‘,[2,3]])) #随机取一个 print(random.sample([1,4,7],2)) #随机取两个 print(random.uniform(2,4)) #2到4的浮点型 a = [1,2,3] #打乱a数组 random.shuffle(a) print(a)
结果:
0.2782936470860382
5
5
7
[7, 4]
2.736930621547071
[2, 3, 1]
生成6位数字的验证码:
def v_code(): res = ‘‘ for i in range(6): num = random.randint(0,9) res += str(num) return res print(v_code())
结果:696520
生成6位数字+字母的验证码:
def v_code(): res = ‘‘ for i in range(6): num = random.randint(0,9) char = chr(random.randint(65,122)) //chr(65)为‘A‘,chr(122)为‘z‘ s = str(random.choice([num,char])) //从两个元素(一个数字,一个字母)的数组中随机取一个 res += s return res print(v_code())
结果:s0309l
标签:大致 nbsp rand def shuffle 验证码 random模块 class div
原文地址:https://www.cnblogs.com/xulan0922/p/10188254.html