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模块详解的更多相关文章

  1. python标准库介绍——27 random 模块详解

    ==random 模块== "Anyone who considers arithmetical methods of producing random digits is, of cour ...

  2. 小白的Python之路 day5 random模块和string模块详解

    random模块详解 一.概述 首先我们看到这个单词是随机的意思,他在python中的主要用于一些随机数,或者需要写一些随机数的代码,下面我们就来整理他的一些用法 二.常用方法 1. random.r ...

  3. Python 单向队列Queue模块详解

    Python 单向队列Queue模块详解 单向队列Queue,先进先出 '''A multi-producer, multi-consumer queue.''' try: import thread ...

  4. Kali linux 2016.2(Rolling)中的payloads模块详解

    不多说,直接上干货! 前期博客 Kali linux 2016.2(Rolling)中的Exploits模块详解 payloads模块,也就是shellcode,就是在漏洞利用成功后所要做的事情.在M ...

  5. Python中操作mysql的pymysql模块详解

    Python中操作mysql的pymysql模块详解 前言 pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同.但目前pymysql支持python3.x而后者不支持 ...

  6. python之OS模块详解

    python之OS模块详解 ^_^,步入第二个模块世界----->OS 常见函数列表 os.sep:取代操作系统特定的路径分隔符 os.name:指示你正在使用的工作平台.比如对于Windows ...

  7. python之sys模块详解

    python之sys模块详解 sys模块功能多,我们这里介绍一些比较实用的功能,相信你会喜欢的,和我一起走进python的模块吧! sys模块的常见函数列表 sys.argv: 实现从程序外部向程序传 ...

  8. python中threading模块详解(一)

    python中threading模块详解(一) 来源 http://blog.chinaunix.net/uid-27571599-id-3484048.html threading提供了一个比thr ...

  9. python time 模块详解

    Python中time模块详解 发表于2011年5月5日 12:58 a.m.    位于分类我爱Python 在平常的代码中,我们常常需要与时间打交道.在Python中,与时间处理有关的模块就包括: ...

随机推荐

  1. (linux)SD卡初始化-mmc_sd_init_card函数(续)

      转自:http://www.cnblogs.com/fengeryi/p/3472728.html   mmc_sd_init_card剩下的关于UHS-I的分支结构. uhs-I的初始化流程图如 ...

  2. leelazero and google colab

    https://github.com/gcp/leela-zero/blob/master/COLAB.md 左侧菜单展开,可以查看细节

  3. Oracle:sequence问题研究

    一直以来,以为sequence是不间断地持续增长的:但今天发现sequence是会跳号,这种情况发生在RAC环境下.在单实例环境下,应该不存在的. sequence截图如下: 数据库表中发生了跳号: ...

  4. SPOJ:Lexicographically Smallest(并查集&排序)

    Taplu and Abhishar loved playing scrabble. One day they thought of inventing a new game using alphab ...

  5. BZOJ_1713_[Usaco2007 China]The Bovine Accordion and Banjo Orchestra 音乐会_斜率优化

    BZOJ_1713_[Usaco2007 China]The Bovine Accordion and Banjo Orchestra 音乐会_斜率优化 Description Input 第1行输入 ...

  6. Mother's Milk

    链接 分析:我们用vis[i][j][k]来记录A,B,C三个状态是否被访问过,同时用s[i]来记录C的所有可能值,当i==0时,如果j合法,则标记s[k]=1,最后统计所有为1的s即可 /* PRO ...

  7. react之fetch请求json数据

    Fetch下载 npm install whatwg-fetch -S Fetch请求json数据 json文件要放在public内部才能被检索到

  8. Watir: 应用Watir-Webdriver 访问需要证书的网站情况

    #Suppose we will access an SVN net require 'watir-webdriver' b = Watir::Browser.new :chrome b.goto ' ...

  9. Linux 系统开机启动项清理

    一般情况下,常规用途的 Linux 发行版在开机启动时拉起各种相关服务进程,包括许多你可能无需使用的服务,例如蓝牙bluetooth.Avahi. 调制解调管理器ModemManager.ppp-dn ...

  10. 【215】◀▶ IDL 文件操作说明

    参考:I/O - General File Access Routines —— 基本文件操作函数 01   CD 修改当前的工作空间路径. 02   FILE_SEARCH 对文件名进行特定的查找. ...