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

    【1】random()  大于0且小于1之间的小数,float

    import random
    print(random.random())
    #0.6929254526372903

    【2】randint(a,b) 大于等于a且小于等于b之间的整数

    print(random.randint(1,6)
    #4

    【3】randrange(a,b) 大于等于a且小于b之间的整数

    print(random.randrange(4,6))
    #4

    【4】choice([a,b,c])  a或者b或者c

    print(random.choice([1,2,3,[4,5]]))
    #2

    【5】sample()

    print(random.sample([1,'23',[4,5]],2))
    #列表元素任意2个组合
    #[1, '23']

    【6】uniform(a,b) 大于a,小于b的小数

    print(random.uniform(2,4))
    #3.1164524618041236

    【7】shuffle(item) 打乱item的顺序

    item = [1,2,3,4,5]
    random.shuffle(item)
    print(item)
    #[1, 4, 5, 2, 3]

    随机验证码

    import random
    def random_code(n):
        res = ''
        for i in range(n):
            #随机转换成ascill码
            str1 = chr(random.randint(65,90))
            str2 = str(random.randint(0,9))
            res += random.choice([str1,str2])
        return res
    print(random_code(8))

  • 相关阅读:
    mysql 版本查看
    js 中文乱码
    浏览器内核
    Squid 代理服务器
    minicygwin
    firefox 插件开发
    ocx c++
    NetBeans
    android 虚拟机
    ExpandableListView
  • 原文地址:https://www.cnblogs.com/Jiangchuanwei/p/8546086.html
Copyright © 2011-2022 走看看