random模块详解
1.import random
random·randint(a,b) 括号里是一个范围,random·randint()是取括号里范围的随机数。
>>> import random
>>> random.randint(1,10)
8
>>>
>>> random.randint(1,10)
4
>>> random.randint(1,10)
2.random.randrange(a,b)
和randint唯一区别就是randrange不包含b,不会随机到b。
3.random.random() 返回一个随机浮点数
random.choice() 括号里的值必须是可以被查找的,如列表,元组,字符串这些可以索引的,然后获得这些值的随机值。
4.random.sample(a,n)a是可索引的数列,n是返回值的个数。random.sample()返回多个值。以列表形式返回。
>>> random.sample([1,2,3,4,5,6,],3)
[4, 5, 2]
>>> random.sample('2jdko3fdls;',5)
[';', 'l', 'j', 'd', '3']
5.验证码的生成
import string
string.ascii_lowercase 英文字符小写
string.digits 数字
string.punctuation 特殊符号
>>> import string
>>> import random
>>> string.digits
'0123456789'
>>> string.ascii_lowercase
'abcdefghijklmnopqrstuvwxyz'
>>> string.punctuation
'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
>>> s = string.ascii_lowercase + string.digits + string.punctuation
>>> s
'abcdefghijklmnopqrstuvwxyz0123456789!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
>>> random.sample(s,5)
['0', 'r', ']', '_', '@']
>>> ''.join(random.sample(s,5))
'g<ve8'
6.random.shuffle(a)
将一个序列重新洗牌
>>> d = [1,2,3,4,5,6,7,8,9,0]
>>> random.shuffle(d)
>>> d
[4, 2, 8, 1, 7, 0, 5, 6, 3, 9]
random模块详解的更多相关文章
- python标准库介绍——27 random 模块详解
==random 模块== "Anyone who considers arithmetical methods of producing random digits is, of cour ...
- 小白的Python之路 day5 random模块和string模块详解
random模块详解 一.概述 首先我们看到这个单词是随机的意思,他在python中的主要用于一些随机数,或者需要写一些随机数的代码,下面我们就来整理他的一些用法 二.常用方法 1. random.r ...
- Python 单向队列Queue模块详解
Python 单向队列Queue模块详解 单向队列Queue,先进先出 '''A multi-producer, multi-consumer queue.''' try: import thread ...
- Kali linux 2016.2(Rolling)中的payloads模块详解
不多说,直接上干货! 前期博客 Kali linux 2016.2(Rolling)中的Exploits模块详解 payloads模块,也就是shellcode,就是在漏洞利用成功后所要做的事情.在M ...
- Python中操作mysql的pymysql模块详解
Python中操作mysql的pymysql模块详解 前言 pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同.但目前pymysql支持python3.x而后者不支持 ...
- python之OS模块详解
python之OS模块详解 ^_^,步入第二个模块世界----->OS 常见函数列表 os.sep:取代操作系统特定的路径分隔符 os.name:指示你正在使用的工作平台.比如对于Windows ...
- python之sys模块详解
python之sys模块详解 sys模块功能多,我们这里介绍一些比较实用的功能,相信你会喜欢的,和我一起走进python的模块吧! sys模块的常见函数列表 sys.argv: 实现从程序外部向程序传 ...
- python中threading模块详解(一)
python中threading模块详解(一) 来源 http://blog.chinaunix.net/uid-27571599-id-3484048.html threading提供了一个比thr ...
- python time 模块详解
Python中time模块详解 发表于2011年5月5日 12:58 a.m. 位于分类我爱Python 在平常的代码中,我们常常需要与时间打交道.在Python中,与时间处理有关的模块就包括: ...
随机推荐
- 学习html5 中的canvas(一)
1.canvas画直线 <!doctype html> <html> <head> <meta charset="UTF-8"> & ...
- bootstrap学习心得
一.html的编写规范 <!DOCTYPE html> <html lang="zh-CN"> <head> <title>Page ...
- [RK3288][Android6.0] 调试笔记 --- 系统识别不同硬件版本方法【转】
本文转载自:http://m.blog.csdn.net/kris_fei/article/details/70226451 Platform: RockchipOS: Android 6.0Kern ...
- android adb 源码框架分析(2 角色)【转】
本文转载自:http://blog.csdn.net/luansxx/article/details/25203323 角色 l 服务 服务是提供特定功能的实体,接收请求,返回应答是服务直接最表现. ...
- POJ3436 ACM Computer Factory —— 最大流
题目链接:https://vjudge.net/problem/POJ-3436 ACM Computer Factory Time Limit: 1000MS Memory Limit: 655 ...
- MAC 开发 stm32 程序
资料:http://www.cnblogs.com/humaoxiao 大神技术.
- javascript 树形菜单
1. [代码][JavaScript]代码 var ME={ini:{i:true,d:{},d1:{},h:0,h1:0,h2:0},html:function(da,f){var s='& ...
- html5--6-63 布局
html5--6-63 布局 实例 学习要点 掌握传统布局与CSS3新增布局方式的实现和应用 掌握CSS3新增属性box-sizing 了解CSS3新增的多列布局 常用布局方式 固定布局与流体布局的优 ...
- I.MX6 Android busybox 从哪里生成的
/**************************************************************************** * I.MX6 Android busybo ...
- 聊聊Java SPI机制
一.Java SPI机制 SPI(Service Provider Interface)是JDK内置的服务发现机制,用在不同模块间通过接口调用服务,避免对具体服务服务接口具体实现类的耦合.比如JDBC ...