python - random模块

随机数模块:

import random

#随机0-1内的浮点数
# print(random.random())

#随机1-100的整数
# print(random.randint(1,100))

#同上类似 1-99 的数
# print(random.randrange(1,100))

#给定数据,随机选择一个数
# a = [1,3,54,5,6,7,4]
# print(random.choice(a))

#给定数据,随机选择x个
# a = [1,3,54,5,6,7,4]
# print(random.sample(a,3))

#随机范围内的浮点数据
# print(random.uniform(1,4))

#打乱顺序
# a = [1,2,3,4,5,6,7,8,9,0]
# random.shuffle(a)
# print(a)

# 应用:
#四位随机验证码:
# def random_yz():
#     rand = ""
#     for i in range(1,5):
#         a = random.randint(0,9)
#         b = chr(random.randint(65,122))
#         c =random.choice([a,b])
#         rand += str(c)
#     return rand
# print(random_yz())

# 拓展:
#chr转换为ascii字符

#48-57为0-9
# for i in range(48,58):
#     print(chr(i))

# #65-90为大写A-Z
# for i in range(65,91):
#     print(chr(i))

# #97-122为小写a-z
# for i in range(97,123):
#     print(chr(i))

 

posted @ 2018-09-23 16:30  Anec  阅读(167)  评论(0编辑  收藏  举报