首页 > 其他 > 详细

random模块

时间:2018-10-04 07:23:28      阅读:97      评论:0      收藏:0      [点我收藏+]

 

技术分享图片
 1 # Author:Sure Feng
 2 
 3 ‘‘‘
 4 常用标准库:random模块学习
 5 ‘‘‘
 6 
 7 import random,string
 8 
 9 # 随机整数
10 print(random.randint(0, 9)) # 前后包含
11 #从指定范围(eg:0~10)中,按指定基数(eg:3)递增的集合中随机选取整数,与range()类似
12 print(random.randrange(0, 19, 3)) # 顾头不顾尾
13 
14 # 随机浮点数
15 print(random.random()) # 随机生成0~1
16 print(random.uniform(0, 4)) # 自定义范围,随机生成浮点数,顾头不顾尾
17 
18 # 随机截取有序类型sequence(字符串、tuple、list)
19 print(random.choice("hello_world")) # 字符串
20 print(random.choice(("I‘m", "the", "King"))) # tuple
21 print(random.choice(["sure", "su", "marry"])) # list
22 print(random.sample("fdaf弹尽粮绝", 4)) # sample(sequence, k), 随机截取k个seque中的数据
module_random
技术分享图片
0
0
0.4860437858831217
3.090356134065184
l
the
marry
[, d, , ]
answer

 

 1 # Author:Sure Feng
 2 
 3 
 4 ‘‘‘
 5 随机生成5位数字或字母的验证码
 6 ‘‘‘
 7 
 8 import random, string
 9 
10 # 定义空的验证码字符串
11 checkcode = ""
12 
13 # 遍历循环5次,并在每次循环后添加新的数字或字母
14 for i in range(0, 5):
15     current = random.randrange(0, 5)
16     # 获取字母
17     if current == i:
18         tmp = chr(random.randint(65, 90))
19     # 获取数字
20     else:
21         tmp = random.randint(0, 9)
22     # 添加字母或数字至验证码
23     checkcode += str(tmp)
24 
25 print(checkcode)

 随机验证码:64K85

random模块

原文:https://www.cnblogs.com/sure-feng/p/9740088.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!