码迷,mamicode.com
首页 > 其他好文 > 详细

day5-随机数相关:random模块&string模块

时间:2017-12-25 20:09:04      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:实现   格式   art   asc   程序   大小写   top   pos   大小   

一、概述

随机数在程序设计中的属于比较基础的内容,主要用于验证场景(如验证码,生成账号对应的密码等),今天结合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
    增强版程序的不足之处在于随机数的长度不固定,有待完善…

day5-随机数相关:random模块&string模块

标签:实现   格式   art   asc   程序   大小写   top   pos   大小   

原文地址:https://www.cnblogs.com/linupython/p/8110875.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!