一、概述

随机数在程序设计中的属于比较基础的内容,主要用于验证场景(如验证码,生成账号对应的密码等),今天结合random模块和string模块来谈谈python中随机数那些事儿。

二、随机数实现相关模块

2.1 random模块

  • random.random()
    返回一个随机浮点数
      1 >>> import random
    2 >>> print(random.random())
    3 0.7998107271564998
    4 >>> print(random.random())
    5 0.7837981404514991
  • random.randint(a,b)
    随机返回a到b之间的一个整型数,注意包括b
      1 >>> print(random.randint(1,3))
    2 1
    3 >>> print(random.randint(1,3))
    4 2
    5 >>> print(random.randint(1,3))
    6 1
    7 >>> print(random.randint(1,3))
    8 3
  • random.randrange(start, stop, step=1)
    返回一个随机整型数,但不包括stop这个值,start和step为可选项,默认值分别为0和1
      1 >>> print(random.randrange(5,step=2))
    2 1
    3 >>> print(random.randrange(5,step=2))
    4 0
    5 >>> print(random.randrange(5,step=2))
    6 1
    7 >>> print(random.randrange(5,step=2))
    8 3
    9 >>> print(random.randrange(1,5,step=2))
    10 1
    11 >>> print(random.randrange(1,5,step=2))
    12 1
    13 >>> print(random.randrange(1,5,step=2))
    14 1
    15 >>> print(random.randrange(1,5,step=2)) #如果start和stop之间的区间太小,然后有设定了start和step,实际取值范围很有限
    16 3
  • randome.sample(population, k)
    从Population中随机抽取k个值来,以列表形式输出。注意这里的Population必须为一个序列或列表。
      1 >>> print(random.sample([1,2,3,4,5],3))
    2 [3, 2, 5]
    3 >>> print(random.sample('asldf9090asm',5))
    4 ['a', 'l', 'd', '9', '0']
    5 >>> print(''.join(random.sample('asldf9090asm',5))) #通过join拼接输出即可得到一般的随机数格式
    6 0da09

2.2 string模块

  • string.ascii_letters
    返回包括所有字母在内的大小写字符串
      1 >>> import string
    2 >>> string.ascii_letters
    3 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
  • string.ascii_lowercase
    返回包含所有小写字母在内的字符串
      1 >>> import string
    2 >>> string.ascii_lowercase
    3 'abcdefghijklmnopqrstuvwxyz'
  • string.ascii_uppercase
    返回包含所有大写字母在内的字符串
      1 >>> import string
    2 >>> string.ascii_uppercase
    3 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    4 >>>
  • string.digits
    返回0-9数字字符串
      1 >>> import string
    2 >>> string.digits
    3 '0123456789'
  • string.punctuation
    以字符串形式返回所有特殊字符
      1 >>> import string
    2 >>> string.punctuation
    3 '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'

三、实战生成随机数

  • 结合random和string实现
      1 >>> import string,random
    2 >>> string2=random.sample(string.ascii_letters+string.punctuation,12)
    3 >>> print(''.join(string2))
    4 _oCSe,)dcBm|
    5 >>>
  • 增强版
    上述程序虽然基本实现了生成随机数的需求,但是随机数的随机性感觉还是比较low,下面来一个增强版的
      1 import random,string
    2 checkcode=''
    3 string1='!@#$%&' #特殊字符
    4 for i in range(4):
    5 current = random.randrange(0,4)
    6 if current != i:
    7 #temp = chr(random.randint(65,99))
    8 temp = ''.join(random.sample(string.ascii_letters+string1, 3))
    9 else:
    10 temp = random.randrange(0,9)
    11 checkcode += str(temp)
    12
    13 print(checkcode)
    14
    15 运行结果:
    16 Lbksxh#ZB%ar

    增强版程序的不足之处在于随机数的长度不固定,有待完善…

  • 固定长度版
    该版本解决上述增强版中随机数长度不固定的问题,看起来更简单
      1 # !/usr/bin/env python
    2 # -*- coding: utf-8 -*-
    3 __author__ = 'Maxwell'
    4
    5 import random,string
    6 checkcode=''
    7 source_string = '@#$%&_'
    8
    9 for i in range(8):
    10 if i % 2 == 0:
    11 temp = ''.join(random.sample(string.ascii_letters + source_string, 2))
    12 else:
    13 temp = str(random.randint(0, 9))
    14 checkcode += temp
    15 print(checkcode)
    16
    17 输出结果:
    18 $G9gp8qg2_C7 #可基本满足随机码的要求了

day5-随机数相关:random模块&string模块的更多相关文章

  1. random模块/string模块

    一.random模块 random模块可以很容易生成随机数和随机字符串. random.randint(1, 100) # 1-100之间取一个随机数 random.randrange(1, 100) ...

  2. Python random模块&string模块 day3

    一.random模块的使用: Python中的random模块用于生成随机数.下面介绍一下random模块中最常用的几个函数. 1.常用函数: (1)random.random() 用于生成一个0到1 ...

  3. random和string模块

    random模块import randomprint(random.random()) #随机打印一个浮点数print(random.randint(1,5)) #随机打印一个整数,包括5print( ...

  4. s14 第5天 时间模块 随机模块 String模块 shutil模块(文件操作) 文件压缩(zipfile和tarfile)shelve模块 XML模块 ConfigParser配置文件操作模块 hashlib散列模块 Subprocess模块(调用shell) logging模块 正则表达式模块 r字符串和转译

    时间模块 time datatime time.clock(2.7) time.process_time(3.3) 测量处理器运算时间,不包括sleep时间 time.altzone 返回与UTC时间 ...

  5. 模块 string 常用序列字符

    自从知道string模块后再也不用abcd了 >>> string.digits #数字 '0123456789' >>> string.ascii_letters ...

  6. Python 3之str类型、string模块学习笔记

    Windows 10家庭中文版,Python 3.6.4, Python 3.7官文: Text Sequence Type — str string — Common string operatio ...

  7. string模块

    string模块 string模块包括了一些字符串常量, 并且有str对象的功能,主要用来生成一些字符串.字符串格式化等 参考: http://python.usyiyi.cn/python_278/ ...

  8. python标准库介绍——4 string模块详解

    ==string 模块== ``string`` 模块提供了一些用于处理字符串类型的函数, 如 [Example 1-51 #eg-1-51] 所示. ====Example 1-51. 使用 str ...

  9. Python之string模块(详细讲述string常见的所有方法)

    相信不少学习python的程序员都接触过string模块 string模块主要包含关于字符串的处理函数 多说无益,初学python的小伙伴还不赶紧码起来 接下来将会讲到字符串的大小写.判断函数. 以及 ...

随机推荐

  1. Python 模块之Logging——常用handlers的使用

    一.StreamHandler 流handler——包含在logging模块中的三个handler之一. 能够将日志信息输出到sys.stdout, sys.stderr 或者类文件对象(更确切点,就 ...

  2. unity3d相关资源

    http://pan.baidu.com/s/1kTG9DVD   GUI源码

  3. linux 学习笔记之文件与管理

    前言: 对于windows来说,文件的系统管理都是非常简单的(这个应该有一个捂脸),通常就是重命名,复制,移动,删除,查看文件属性,查看文件内容,寻找文件.其实在图形化行中的linux也是有这样子功能 ...

  4. pandas(四)唯一值、值计数以及成员资格

    针对Series对象,从中抽取信息 unique可以得到Series对象的唯一值数组 >>> obj = Series(['c','a','d','a','a','b','b','c ...

  5. python mysqldb 模块学习

    一.安装(环境win7 .python2.7) Python2.x 版本,使用MySQL-python: 安装包:MySQL-python-1.2.5.win32-py2.7.exe(双击安装) 下载 ...

  6. Visual Studio 2012的Windows Service服务安装方式

    windows服务应用程序是一种长期运行在操作系统后台的程序,它对于服务器环境特别适合,它没有用户界面,不会产生任何可视输出,任何用户输出都回被写进windows事件日志.计算机启动时,服务会自动开始 ...

  7. Java最新趋势之Spring阅读

    (原文地址:点我) This Week in Spring: Cloud Native and the State of Java This compilation of news and tutor ...

  8. Lightroom’s Clarity Slider – What Does It Do?

    http://digital-photography-school.com/lightrooms-clarity-slider-what-does-it-do/ With the introducti ...

  9. bower安装使用、git安装、node安装、weui安装开发

    bower安装使用以及git安装 bower需要:node 和 git 1.Git安装:(选择第二项:Use Git from the Windows Command Prompt)2.node安装: ...

  10. SpringMVC下文件的上传与下载以及文件列表的显示

    1.配置好SpringMVC环境-----SpringMVC的HelloWorld快速入门! 导入jar包:commons-fileupload-1.3.1.jar和commons-io-2.4.ja ...