random模块用来生成随机数,有以下几个常用方法:

import random
random.random() #产生随机数[0-1)
random.randint(a,b) #产生随机整数[a,b]
random.randrange(a,b,step) #从a-b范围内,按step递增的集合中获取一个随机数。
random.shuffle(a) #将一个列表中元素重洗牌
random.saple(a,b) #从列表a中随机选取b个元素后返回一个列表

我们可以用random模块来生成随机的验证码

 import random
def check_code(n): #n为验证码的位数
check_code = ""
for i in range(n):
tem = random.randrange(n)
if i==tem: #随机位生成数字或字母
code = chr(random.randint(65,90)) #chr()函数将随机生成的65-90为ASCII码对应的A-Z
check_code+=code
else:
code = str(random.randint(0,9))
check_code += code
return check_code

pytho常用模块2——random的更多相关文章

  1. python常用模块之random模块

    python常用模块之random模块 在程序中很多会用到随机字符,比如登陆网站的随机验证码,通过random模块可以很容易生成随机字符串 1.random.randrange():返回1-10之间的 ...

  2. python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib subprocess logging re正则

    python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib  subprocess ...

  3. python 常用模块之random,os,sys 模块

    python 常用模块random,os,sys 模块 python全栈开发OS模块,Random模块,sys模块 OS模块 os模块是与操作系统交互的一个接口,常见的函数以及用法见一下代码: #OS ...

  4. python 常用模块(一): random , time , sys , os模块部分知识.

    1.常用模块:(1)collectiaons模块 (2)与时间相关  time模块 (3)random模块 (4)os模块 (5)sys模块 (6) 序列化模块: json  ,   pickle 2 ...

  5. Python常用模块之random和time

    常用模块: time: 分为三种格式: 1.时间戳:从1970年1月1日0点0分0秒到现在经过的秒数 用于时间间隔的计算 import time print(time.time()) 2.字符串显示时 ...

  6. 常用模块----time&random&hushlib&os

    模块:本质就是一个.py文件 分为三部分: 内置模块 第三方模块 自定义模块(模块调用,包) 加载顺序:内置模块——>自定义模块   time 模块 # <1> 时间戳 >&g ...

  7. python常用模块之random

    random模块 import random print(random.random())#(0,1)----float 大于0且小于1之间的小数 print(random.randint(1,3)) ...

  8. python常用模块之-random模块

    random模块顾名思义就是生成随机数的模块. random模块有以下常见方法: 1,打印0-1之间的任意随机浮点数,不能指定区间. print(random.random()) 2,打印随机符点数, ...

  9. 常用模块(random)

    import randomimport string# dt = random.randint(1,2) # 从1-2间取随机数,包括1.2# dt = random.randrange(1,3) # ...

随机推荐

  1. 03_java基础(一)之计算机应用知识普及

    1.计算机(Computer) 全称:电子计算机,俗称电脑.是一种能够按照程序运行,自动.高速处理海量数据的现代化智能电子设备.由硬件和软件所组成,没有安装任何软件的计算机称为裸机.常见的形式有台式计 ...

  2. Dictionary在多线程情况下

    Add时出错 错误信息: Index was outside the bounds of the array. 详细信息: at System.Collections.Generic.Dictiona ...

  3. encodeURI & encodeURIComponent

    [encodeURI & encodeURIComponent]  区别在于,"&", "+", 和 "=" 不会被enco ...

  4. 批量更新list<string,string>

    public void UpdateList(List<MysqlModule.Model.pro_premanifest> modelList) { List<MySqlParam ...

  5. sqlserver 死锁相关

    参考 https://www.cnblogs.com/fuyuanming/p/5783421.html -- 查询死锁 select request_session_id spid, OBJECT_ ...

  6. IronPython 的几个问题

    1.在脚本中使用datagridview.Rows[i].Cells[1].Value并将其转换为string时,遇到int类型 有时可是直接使用.toString()转换为字符 有时必须采用str( ...

  7. zabbix_server.conf 详解

    # This is a configuration file for Zabbix server daemon # To get more information about Zabbix, visi ...

  8. 江西财经大学第一届程序设计竞赛 H题 求大数的阶乘

    链接:https://www.nowcoder.com/acm/contest/115/H 来源:牛客网 晚上,小P喜欢在寝室里一个个静静的学习或者思考,享受自由自在的单身生活. 他总是能从所学的知识 ...

  9. 打印低头思故乡 java

    public static void main(String args[][){ char poet[] = str.tocharArray(); int pos = 18; while(true){ ...

  10. HDU4522 湫湫系列故事——过年回家

    传送门:点我 中文题面. 思路:拿spfa对卧铺和硬铺分别跑spfa,然后找两个的最短路.体感堆优化的dij也可以,不过spfa跑跑就过去了.有个细节是最后得用long long 存数据,其他的没啥. ...