zoukankan      html  css  js  c++  java
  • random(1)

    import random
    
    print(random.random())#(0,1)----float 大于0且小于1之间的小数
    
    print(random.randint(1,3)) #[1,3] 大于等于1且小于等于3之间的整数
    
    print(random.randrange(1,3)) #[1,3) 大于等于1且小于3之间的整数
    
    print(random.choice([1,'23',[4,5]]))#1或者23或者[4,5]
    
    print(random.sample([1,'23',[4,5]],2))#列表元素任意2个组合
    
    print(random.uniform(1,3))#大于1小于3的小数,如1.927109612082716 
    
    
    item=[1,3,5,7,9]
    random.shuffle(item) #打乱item的顺序,相当于"洗牌"
    print(item)
    

      

    random实际用的不多,主要是随机一个数,或者字符,做验证码,或者爬虫里用作随机时间 

    import random
    def make_code(n):
        res=''
        for i in range(n):
            s1=chr(random.randint(65,90))
            s2=str(random.randint(0,9))
            res+=random.choice([s1,s2])
        return res
    
    print(make_code(9))
    
    生成随机验证码
  • 相关阅读:
    适配器模式(Adapter)
    状态模式(State)
    观察者模式(Publish/Subscribe)
    建造者模式(Builder)
    数据库,知识点汇总
    数据库
    css样式大全
    遮罩层
    js数组冒泡
    js基本方法
  • 原文地址:https://www.cnblogs.com/shanjinghao/p/9169134.html
Copyright © 2011-2022 走看看