zoukankan      html  css  js  c++  java
  • random模块

     1 # Author:Sure Feng
     2 
     3 '''
     4 常用标准库:random模块学习
     5 '''
     6 
     7 import random,string
     8 
     9 # 随机整数
    10 print(random.randint(0, 9)) # 前后包含
    11 #从指定范围(eg:0~10)中,按指定基数(eg:3)递增的集合中随机选取整数,与range()类似
    12 print(random.randrange(0, 19, 3)) # 顾头不顾尾
    13 
    14 # 随机浮点数
    15 print(random.random()) # 随机生成0~1
    16 print(random.uniform(0, 4)) # 自定义范围,随机生成浮点数,顾头不顾尾
    17 
    18 # 随机截取有序类型sequence(字符串、tuple、list)
    19 print(random.choice("hello_world")) # 字符串
    20 print(random.choice(("I'm", "the", "King"))) # tuple
    21 print(random.choice(["sure", "su", "marry"])) # list
    22 print(random.sample("fdaf弹尽粮绝", 4)) # sample(sequence, k), 随机截取k个seque中的数据
    module_random
    0
    0
    0.4860437858831217
    3.090356134065184
    l
    the
    marry
    ['', 'd', '', '']
    answer
     1 # Author:Sure Feng
     2 
     3 
     4 '''
     5 随机生成5位数字或字母的验证码
     6 '''
     7 
     8 import random, string
     9 
    10 # 定义空的验证码字符串
    11 checkcode = ""
    12 
    13 # 遍历循环5次,并在每次循环后添加新的数字或字母
    14 for i in range(0, 5):
    15     current = random.randrange(0, 5)
    16     # 获取字母
    17     if current == i:
    18         tmp = chr(random.randint(65, 90))
    19     # 获取数字
    20     else:
    21         tmp = random.randint(0, 9)
    22     # 添加字母或数字至验证码
    23     checkcode += str(tmp)
    24 
    25 print(checkcode)

     随机验证码:64K85

  • 相关阅读:
    【linux 文件管理】7-文件重定向
    by David Bombal CCNA with kali linux
    【linux 文件管理】6-软链接和硬链接
    13.mysql索引的使用
    11.mysql SQL优化之SQL问题定位
    mysql服务常用命令
    10.mysql存储引擎
    9. Mysql的体系结构概览
    8.mysql触发器
    项目上线部署
  • 原文地址:https://www.cnblogs.com/sure-feng/p/9740088.html
Copyright © 2011-2022 走看看