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

    random模块:   
      >>> import random
      >>> random.random()          #0到1之间的数      (0, 1);
      0.16533475988055923
      >>>
     
      >>> random.randint(1,3)         #1到3之前的整数  [1, 3]       
      1
      >>>
      >>> random.randint(1,3)
      3
      >>> random.randint(1,3)
      2
      >>> random.randint(1,3)
      3
      
      
      >>> random.randrange(1, 3)        #1到3之前的整数  [1, 3)
      1
      >>> random.randrange(1, 3)
      2
      >>> random.randrange(1, 3)
      1
      >>> random.randrange(1, 3)
      1
      >>>
      
     
    1.   随机选取字符串
           >>> random.choice("hello")    参数需要是序列,字符串,元组,列表。
           'l'
      
    2.   多个字符串选取特定数量的字符
          >>> random.sample("asasas", 2)         #指定长度取,从字符串中随机取2位
         ['a', 'a']
      
      >>> random.uniform(1,3)
      1.1422713831096774
      >>> random.uniform(1,3)
      2.043928712313657
      >>> random.uniform(1,3)
      2.1317285111885793
      >>> random.uniform(1,3)
      2.106939191769194
      >>>
     
     
      random.shuffle        #洗牌功能:
      >>> a = [1,2,3,4,5,6,7]
      >>> random.shuffle(a)
      >>> a
      [2, 7, 4, 1, 5, 6, 3]
      >>> random.shuffle(a)
      >>> a
      [3, 7, 5, 4, 2, 1, 6]
      >>>
      
      
      生成随机验证码:
      
      import random
       check_code = ""
       for i in range(6):
        current = random.randrange(0, 6)
        if i == current:
         check_code += chr(random.randint(65, 90))
        else:
         check_code += str(random.randint(0, 9))
       print(check_code)
  • 相关阅读:
    电路开发软件积累
    .NET移动开发环境搭建
    Microsoft SQL Server Express各版本对比
    .NET IL指令速查表
    C#访问加密的SQLite数据库
    C#中时间计算方法汇总
    最佳C/C++编辑器 source insight3
    常用PHP框架收集
    CSS 中文字体的英文名称 (simhei, simsun) 宋体 微软雅黑等
    Windows下Java JDK8配置环境变量
  • 原文地址:https://www.cnblogs.com/brace2011/p/9226358.html
Copyright © 2011-2022 走看看