• 随机数random


    import random
    
    print(random.random())  #不接收任何参数
    print(random.randint(5,9))  #接收参数范围包含边界值
    # random.randrange(10)
    print(random.randrange(10,15)) #可接受一个和两个
    print(random.choice("abcd"))   #可以接受一个参数,字符串也可以放列表字典等
    print(random.choice(["abc",[1,2,3]]))
    # print(random.choices(["fefewf"]))
    print(random.sample(["123",4,[123],2],2)) #可以从里面随机选几个,最后一个数字是选择的个数
    
    print(random.choice(["a","b","d","e",1,2,3,4,5]))

    #下面两种方式实现5位数的随机数
    def ff():
        verification_code = ""
        for i in range(6):
            randomdigst = str(random.choice([chr(random.randrange(65,91)),random.randrange(1,9)]))
            verification_code += randomdigst
        print(verification_code)
    ff()
    def ff():
        verification_code = ""
        for i in range(6):
            if i == random.randrange(0,3):
                randomdigst = str(random.randrange(1,9))
            else:
                randomdigst = chr(random.randrange(65,91)).lower()
            verification_code += randomdigst
        print(verification_code)
    ff()
    
    
     
  • 相关阅读:
    CSU1784
    HDU 6128 Inverse of sum(数学)
    容斥原理入门
    HDU 6129 Just do it(规律)
    HDU 6140 Hybrid Crystals(zz)
    HDU 6143 Killer Names(排列+容斥,dp)
    zzuli 2177 Contest
    zzuli 2180 GJJ的日常之沉迷数学(逆元)
    除法逆元入门
    乘法逆元数论篇
  • 原文地址:https://www.cnblogs.com/TKOPython/p/11749072.html
Copyright © 2020-2023  润新知