python的random模块用于生成随机数,下面介绍一下random模块的常用方法:

取随机小数:  数学计算
random.random() 用于生成一个0-1的随机浮点数 0<=n<1.0
random.uniform(a,b) 生成一个指定范围内的随机浮点数, a<=n<=b
取随机整数: 彩票 抽奖
random.randint(a,b)       取一个指定范围内的整数 a<=n<=b
random.randrange(start,stop,step) 在指定范围内,按基数递增的集合内取一个随机数,如random.randrange(10,100,2),结果相当于从[10,12,14,...98]序列中获取一个随机数。
从一个序列中随机取值: 抽奖
random.choice() 从序列中随机选择一个返回个数为
random.sample() 从序列中随机选择多个返回,返回的个数为函数的第二个参数
乱序:
random.shuffle()  打乱一个列表的顺序,在原列表的基础上直接进行修改,节省空间
验证码的生成:

6位数字验证码:
s = ''
for i in range(6):
num = random.randint(0,9)
s += str(num)
print(s) 函数版本的:
def code(n=6):
s = ''
for i in range(n):
num = random.randint(0,9)
s += str(num)
return s
print(code(4))
print(code()) 6位数字+字母验证码:
def code(n = 6):
s = ''
for i in range(n):
# 生成随机的大写字母,小写字母,数字各一个
num = str(random.randint(0,9))
alpha_upper = chr(random.randint(65,90))
alpha_lower = chr(random.randint(97,122))
res = random.choice([num,alpha_upper,alpha_lower])
s += res
return s
print(code(4))
print(code()) 进阶:
def code(n = 6,alpha = True):
s = ''
for i in range(n):
num = str(random.randint(0,9))
if alpha:
alpha_upper = chr(random.randint(65,90))
alpha_lower = chr(random.randint(97,122))
num = random.choice([num,alpha_upper,alpha_lower])
s += num
return s
print(code(4,False))
print(code(alpha=False))

python ranndom模块及生成验证码的更多相关文章

  1. python的random模块(生成验证码)

    python的random模块(生成验证码) random模块常用方法 random.random() #生成0到1之间的随机数,没有参数,float类型 random.randint(1, 3) # ...

  2. re随机模块应用-生成验证码(无图片)

    方法一,通过choice方式生成验证码 此方法生成每次调用crate_code()会生成三个随机数,然后再三个随机数中选择一个,资源调用相对多些 import random def v_code(co ...

  3. python练习题之随机生成验证码

    #引用random模块下的randint项目#定义验证码函数.定义一个空字符串变量,分三种情况,随机产生的大写字母,随机产生的小写字母,随机产生的数字.然后#每一次执行哪一种情况,条件也是随机的,就是 ...

  4. Python学习笔记_生成验证码

    import random def verification_code(): num = [str(x) for x in range(10)] # 列表生成器0-9 upper = [chr(x) ...

  5. Python 常用模块系列学习(1)--random模块常用function总结--简单应用--验证码生成

    random模块--random是一个生成器 首先: import random    #导入模块 print (help(random))    #打印random模块帮助信息 常用function ...

  6. Python 使用Pillow模块生成验证码

    1.安装 pip3 install pillow 2.使用步骤 生成验证码和验证字符串 绘制图片,将验证码放入session中 将图片返回给页面 3.代码demo #!/usr/bin/env pyt ...

  7. 使用模块PIL 生成 随机验证码

    --------------默认自己无能,无疑是给失败制造机会!你认为自己是什么样的人,就将成为什么样的人. 要使用PIL模块. 安装: 1 pip3 install pillow 基本使用 1. 创 ...

  8. Django 生成验证码或二维码 pillow模块

    一.安装PIL PIL:Python Imaging Library,已经是Python平台事实上的图像处理标准库了.PIL功能非常强大,API也非常简单易用.   PIL模块只支持到Python 2 ...

  9. Python图片处理PIL/pillow/生成验证码/出现KeyError: 和The _imagingft C module is not installed

    最近在用Python开发自己的博客,需要用到Python生成验证码,当然肯定要用到Python的图形处理库PIL,因为我用的是windows. 所以在安装好pil之后就开始写,就按照题目所说出现了Th ...

随机推荐

  1. Android开发基础

    一.Android开发环境搭建 1.下载安卓SDK 官方下载地址:http://developer.android.com/sdk/index.html 2.下载安装JDK 官方下载地址:JDK6 h ...

  2. android sqlite3命令行检查自己的代码操作数据库是否正确

    真机调试的话需要root ,否则没有访问目录的权限 在 linux 的终端 或者 windows的cmd 中输入 adb shell 进入shell 环境 cd /data/data/程序包名/dat ...

  3. ansible使用9-Playbooks: Special Topics

    Accelerated Mode port 5099 持续连接30min --- - hosts: all accelerate: true tasks: - name: some task comm ...

  4. Linux文件属性与权限

    一.在Linux里面,任何一个文件都具有“User,Group,Others”(用户.用户组.其他人)三种身份 二.用户组最有用的功能之一,就是当你在团队开发资源的时候,且每个账号都可以有多个用户组的 ...

  5. 来自NVIDIA开源的pix2pixHD,将Image-to-Image Translation带到了另一个境界

    Kuo Ming Lin 分享了 Learning By Hacking 的动图 最近討論最火熱的project之一,來自NVIDIA開源的pix2pixHD,將Image-to-Image Tran ...

  6. ubuntu linux查看cpu信息

    $ cat /proc/cpuinfo CPU核心数量 $ grep -c processor /proc/cpuinfo

  7. 如何启用SAP C4C OData Event Notification

    当我们在试图使用SAP C4C OData事件通知这个功能时,如果遇到下列提示消息,说明这个功能在business configuration里没有开启: The OData Event Notifi ...

  8. Uva 11384 正整数序列

    题目链接:https://vjudge.net/problem/UVA-11384 题意:给定正整数 n,用最少的操作把序列 1,2,,,n 全部变成 0: 操作是:每次可以从序列中选择一个或者多个, ...

  9. (转载)Fiddler模拟post四种请求数据

    https://www.cnblogs.com/xiaoxi-3-/p/7612254.html https://blog.csdn.net/qq_15283475/article/details/5 ...

  10. 2017.11.16 JavaWeb-------第八章 EL、JSTL、Ajax技术

    第八章 EL.JSTL.Ajax技术 ~~ EL (expression language) 是表达式语言 ~~ JSTL(JSP Standard Tag Library) 是开源的JSP标准标签库 ...