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

    random() 方法返回随机生成的一个实数,它的范围在[0,1)范围内

    random()不能直接访问,需要导入random模块,然后通过random静态对象调用该方法

    import random
    
    print(random.random())      #随机生成(0,1)的小数
    
    print(random.randint(1,3))  #[1,3]的整数
    
    print(random.randrange(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的小数
    
    
    
    item=[1,3,5,7,9]
    random.shuffle(item)    #打乱item的顺序,相当于'洗牌'
    print(item)
    
    #运行结果
    0.565599027255551
    2
    1
    23
    [1, '23']
    1.1400100074488329
    [1, 3, 9, 5, 7]
    # 随机生成验证码
    
    def make_code(max_size=5):
        res = ''
        for i in range(max_size):
            num = str(random.randint(0, 9))
            alp = chr(random.randint(65, 90))
    
            res += random.choice([num, alp])
    
        return res
    
    
    print(make_code(10))
  • 相关阅读:
    ubuntu svn
    node install
    Hello World
    复合过去式
    Fréquence
    HTTP是什么?
    Photon——Requirements 需求
    Ext.Net 实现文件下载
    我的绝世好剑——如何开始创建.NET程序
    Photon——Introduction 介绍
  • 原文地址:https://www.cnblogs.com/kp1995/p/10073634.html
Copyright © 2011-2022 走看看