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. Visual Studio 编辑器打开项目后,一直提醒Vs在忙,解决方法

    今天打开VS2015后,因为这个解决中有很项目,突然就一直现在加载中,点击VS提示在忙,怎么破那?请往下看 第一种方法 1.关闭VS: 2.去C:\Users\<your users name& ...

  2. C++ POD类型

    POD( Plain Old Data)概念: Arithmetic types (3.9.1), enumeration types, pointer types, and pointer to m ...

  3. OpenGL学习 Following the Pipeline

    Passing Data to the Vertex Shader Vertex Attributes At the start of the OpenGL pipeline,we use the i ...

  4. *521. Longest Uncommon Subsequence I (bit manipulation 2^n)

    Given a group of two strings, you need to find the longest uncommon subsequence of this group of two ...

  5. 解决SD卡频繁读写问题 Anything-sync-daemon 映射linux目录到tmpfs并定时同步

    Anything-sync-daemon (asd) is a is a diminutive pseudo-daemon designed to manage target directories ...

  6. 【转】Android开发学习笔记(一)——初识Android

    对于一名程序员来说,“自顶向下”虽然是一种最普通不过的分析问题和解决问题的方式,但其却是简单且较为有效的一种.所以,将其应用到Android的学习中来,不至于将自己的冲动演变为一种盲目和不知所措. 根 ...

  7. 在使用HTMLTestRunner时,报告为空,错误提示<_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf_8'>

    <_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf_8'> Time Elapsed: 0:00:21.3163 ...

  8. SVN知识集合

    1. 如果某个项目之前保存了A仓库的信息,无法切换到B仓库(通过AnkhSVN或者VisualSVN),可以先在本地去除版本控制(用TortoiseSVN),然后导出B仓库信息(用TortoiseSV ...

  9. EF 集合版 增删查改

  10. loss 和accuracy的关系梳理

    最近打算总结一下这部分东西,先记录留个脚印.