017random模块

import  random
print(random.random())
print(random.randint(1,8))            #包括8         
print(random.choice('aiq'))          # aiq随便一个
print(random.randrange(1,10))        #不包括10
print(random.sample([1,2,3,4,5],2))  # 可以选择随机多少个

a=[1,2,3,4,5,6]
random.shuffle(a)
print(a)                     # [4, 3, 5, 6, 2, 1]       

#简单生成验证码

1 def  v_code():
2     code=''
3     for  i  in  range(5):
4         if  random.randint(0,4) % 2:
5             code += str(random.randrange(10))
6         else:
7             code += chr(random.randrange(65,91))       #生成A-Z的一个字母
8     print(code)
9 v_code()
View Code
1 def  v_code():
2     code=''
3     for  i  in  range(5):
4         code+=random.choice([str(random.randrange(10)),chr(random.randint(65,91))])
5     print(code)
6 
7 v_code()
View Code

 

骚操作

 1 import  random
 2 a=[2,3,4,5,6,7,8,9]
 3 while  1:
 4     random.shuffle(a)           ## 打乱里面的排序
 5     b = 1000+a[0]*10+a[1]
 6     c = a[2]*1000+a[1]*100+a[3]*10+a[4]
 7     d = 10000+a[3]*100+a[1]*10+a[5]
 8     if  b+c == d:
 9         print("%d%d%d"%(b,c,d))
10         break

 

posted @ 2017-12-28 13:34  Alos403  阅读(162)  评论(0编辑  收藏  举报