Python函数-random模块

时间:2020-08-15 00:45:50   收藏:0   阅读:89

一、random模块

       程序中有很多的地方需要用到随机字符,比如登录网站的验证码,通过random模块可以很容易的生成随机字符串

import random

number = random.randint(1, 100)  # 随机整数

# random  四个方法
# uniform
f = random.uniform(1, 100)  # 随机小数
print(f)
# choice
s = 12345
l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
t = [12, 3, 45]
print(random.choice(l))  # 从你给的数据(字符串\list都可)里面随机选择一个
# sample
result = random.sample(l, 10)  # 随机从list里面取N个元素
print(result)
# shuffle
l = [str(i) for i in range(1, 14)]
random.shuffle(l)  # 洗牌,打乱
print(l)

运行结果:

14.366001711003156
2
[6, 4, 7, 1, 10, 2, 5, 9, 8, 3]
[6, 10, 3, 2, 9, 4, 7, 8, 1, 12, 5, 13, 11]

 

原文:https://www.cnblogs.com/ccxm/p/13507092.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!