Random模块

random模块用来生成随机的数字或字母

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#-*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"
 
import random
 
print random.random()  #随机0-1之间的数
print random.uniform(1,10#随机1-10之间的数
 
print random.randint(1,10#随机1-10之间的整数
print random.randrange(1,10) #随机1-9之间的整数
 
print random.choice('hello') #从字符串或列表中随机取
print random.sample('fuck you',2) #随机取两个字母
 
#随机列表
a = [1,2,3,4,5,6]
random.shuffle(a)
print a

用random写个小程序,生成随机验证码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#-*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR"
 
import random
 
checkcode = ''
 
for i in range(5):
    current = random.randint(0,5)
    if current == i:
        tmp = chr(random.randint(65,90))
    else:
        tmp = random.randint(0,9)
    checkcode += str(tmp)
 
print (checkcode)

小程序的执行效果

 

posted @   Sch01aR#  阅读(221)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示