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

    常用模块之

    random(随机模块)

    常用方法

    #随机获取1-9中任意的整数
    import random
    res = random.randint(1, 9)
    print(res)
    
    #默认随意获取0-1之间任意小数
    import random
    res1 = random.random()
    print(res1)
    
    #洗牌,将可迭代对象中的值进行乱序
    import random
    list1 = ['红桃6', '方片8', '梅花Q', '红桃K']
    random.shuffle(list1)
    print(list1)
    
    
    #练习,需求:大小写字母 数字组合5位数的随机验证码
    #前置技术:chr() #可以将ASCII表中值转换成对应的字符
    import random
    def get_code(): #定义函数
        code ='' #定义空字符串
        for i in range(5): #按照需求进行5次for循环
            res1 = random.randint(97, 122) #随机取出97-122中的数字
            low_str = chr(res1) #利用chr对照ASCII取出对应的字符
            res2 = random.randint(65, 90) #随机取出65-90中的数字
            upper_str = chr(res2) #利用chr对照ASCII取出对应的字符
            number = str(random.randint(0, 9)) #随机取出0-9中的任意数字,并将其转化为字符串类型
            code_list = [low_str, upper_str, number] #将得到的三部分组成一个新列表
            random_code = random.choice(code_list) #定义randow_code为随机从新列表中取出的值
            code += random_code #将每次取出的字符串进行拼接
        return code #将循环拼接后的字符串返回
    
    
    code = get_code() #定义变量名code为调用函数get_code得到的结果
    print(code) #输出code
    print(len(code))#查看code长度
    
  • 相关阅读:
    在请求中使用XML Publisher生成文件报错
    Oracle Sourcing Implementation and Administration Guide(转)
    API To Import Negotiations(转)
    使用POI动态更新导出的EXCEL模板中的列
    使用POI设置导出的EXCEL锁定指定的单元格
    QML获取随机颜色
    Access导出excel
    Component
    QML中打印
    Qt Quick Dialogs
  • 原文地址:https://www.cnblogs.com/a736659557/p/11894036.html
Copyright © 2011-2022 走看看