Python常用模块-随机数模块(random)
Python常用模块-随机数模块(random)
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.常用方法举例
#!/usr/bin/env python
#_*_conding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie import random
from string import ascii_lowercase import random #返回1-5之间的整数,即取值[1,5]的int类型。
print(random.randint(1,5)) #从非空序列的元素中随机挑选一个元素
print(random.choice(range(10))) #取值[1,3)的int类型。
print(random.randrange(1,3)) #从指定范围内,按指定基数递增的集合中获取一个随机数,基数缺省值为1
print(random.randrange(1,9,2)) num_list = [1,2,3,4,5]
print(num_list) #就地打乱列表元素
random.shuffle(num_list)
print(num_list) #从样本空间或总体(序列或者集合类型)中随机取出k个不同的元素,返回一个新的列表
print(random.sample(['a', 'b', 'c', 'd','e','f','g'], 3))
print(random.sample(['a', 'a'], 2)) #和sample功能类似,只不过choices方法可以随机选择1-10个不同的元素(返回的个数在咱们的规定范围内)
print("".join(random.choices(ascii_lowercase,k=random.randint(1,10)))) #取值(0,1)float类型。
print(random.random()) names = ["yinzhengjie","尹正杰","yzj","北京","西安"] #从给定的列表中随机取一个数字。
print(random.choice(names)) # 从给定的列表中随机取3个元素。
print(random.sample(names,3)) #取值(1,3)的float类型。
print(random.uniform(1,3))
3
4
2
7
[1, 2, 3, 4, 5]
[3, 2, 5, 4, 1]
['c', 'g', 'f']
['a', 'a']
ajjwuyuftf
0.0900107743331563
北京
['尹正杰', 'yinzhengjie', '北京']
1.695671638754336
以上代码执行结果
二.验证码案例
#!/usr/bin/env python
#_*_coding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
#EMAIL:y1053419035@qq.com import random def ValdateCode(number=5):
res = ""
for i in range(number):
num = random.randint(1, 9)
string = chr(random.randint(97,122))
s = random.choice([str(num),string])
res += s
return res res = ValdateCode(10)
print(res) #以上代码执行结果如下:
43jh12l2i5
Python常用模块-随机数模块(random)的更多相关文章
- Python常用内建模块
Python常用内建模块 datetime 处理日期和时间的标准库. 注意到datetime是模块,datetime模块还包含一个datetime类,通过from datetime import da ...
- python模块 | 随机数模块—random模块
python随机数模块 random - 生成伪随机数,该模块实现了各种分布的伪随机数生成器. 对于整数,从范围中有统一的选择. 对于序列,存在随机元素的统一选择.用于生成列表的随机排列的函数.以及用 ...
- python常用内建模块 collections,bs64,struct,hashlib,itertools,contextlib,xml
# 2 collections 是Python内建的一个集合模块,提供了许多有用的集合类. # 2.1 namedtuple #tuple可以表示不变集合,例如,一个点的二维坐标就可以表示成: p ...
- Python 常用内建模块(time ,datetime)
1,在Python中,与时间处理有关的模块就包括:time,datetime以及calendar. 2,在Python中,通常有这几种方式来表示时间:1)时间戳 2)格式化的时间字符串 3)元组(st ...
- python常用命令—查看模块所在位置
环境:ipython3 交互式解释器 语法: import 模块名 模块名.__file__ 功能: 查看模块的所在位置 例:
- Python 常用内建模块(os, sys,random)
一.os 模块 1,操作系统与环境变量 import osprint(os.name) #操作系统类型,如果是posix 说明系统是linux unix 或 mac os x :如果是nt 就是win ...
- python常用函数及模块
原文来源于博客园和CSDN 1.计算函数 abs()--取绝对值 max()--取序列最大值,包括列表.元组 min()--取序列最小值 len()--取长度 divmod(a,b)---取a//b除 ...
- python常用内建模块——datetime
datetime是python处理日期和时间的标准库. 获取当前日期和时间 >>>from datetime import datetime >>>now = da ...
- collections(python常用内建模块)
文章来源:https://www.liaoxuefeng.com/wiki/897692888725344/973805065315456 collections collections是Python ...
随机推荐
- fiddler之会话数据的修改
fiddler之会话数据的修改 fiddler记录http的请求,并且针对特定的http请求,可以分析请求数据.修改数据.调试web系统等,功能十分强大.本篇主要讲两种修改的数据的方法,断点和Unlo ...
- 小程序swiper组件高度自适应【转载】
最近在做小程序开发,复制官方文档上的swiper组件实测后发现,图片不能自适应.网上找了几个版本测试都发现存在一些小问题,目前这个版本本人实测是最好用的.记录一下,方便日后使用. 感谢原创大神的帮助, ...
- 《Pro SQL Server Internals, 2nd edition》的CHAPTER 3 Statistics中的Introduction to SQL Server Statistics、Statistics and Execution Plans、Statistics Maintenance(译)
<Pro SQL Server Internals> 作者: Dmitri Korotkevitch 出版社: Apress出版年: 2016-12-29页数: 804定价: USD 59 ...
- 比较 VGG, resnet和inception的图像分类效果
简介 VGG, resnet和inception是3种典型的卷积神经网络结构. VGG采用了3*3的卷积核,逐步扩大通道数量 resnet中,每两层卷积增加一个旁路 inception实现了卷积核的并 ...
- hive orc压缩数据异常java.lang.ClassCastException: org.apache.hadoop.io.Text cannot be cast to org.apache.hadoop.hive.ql.io.orc.OrcSerde$OrcSerdeRow
hive表在创建时候指定存储格式 STORED AS ORC tblproperties ('orc.compress'='SNAPPY'); 当insert数据到表时抛出异常 Caused by: ...
- LeetCode 633. 平方数之和
题目: 给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a2 + b2 = c. 示例1: 输入: 5 输出: True 解释: 1 * 1 + 2 * 2 = 5 示例2 ...
- C# winform打开文件夹并选中指定文件
例如:打开“E:\Training”文件夹并选中“20131250.html”文件 System.Diagnostics.Process.Start("Explorer.exe", ...
- HTML5之HTTP协议
---恢复内容开始--- 99%的人都理解错了HTTP中GET与POST的区别 2016.10.11 13:23:22来源: 51cto作者:51cto (转) GET和POST是HTTP请求 ...
- Android map转json格式,附上Jackson包下载地址,导入过程
android中的map转json,需要下载jackson包,下载地址: http://www.java2s.com/Code/Jar/j/Downloadjacksonall199jar.htm 下 ...
- array_pop()方法
array_pop — 将数组最后一个单元弹出(出栈) 说明 mixed array_pop ( array &$array ) array_pop() 弹出并返回 array 数组的最后一个 ...