zoukankan      html  css  js  c++  java
  • Python-random 随机数模块

    random 随机数模块
    格式:
      import random 引入随机模块文件

    1 import random
     2  
     3 print(random.random())#(0,1)----float    大于0且小于1之间的小数
     4  
     5 print(random.randint(1,3))  #[1,3]    大于等于1且小于等于3之间的整数
     6  
     7 print(random.randrange(1,3)) #[1,3)    大于等于1且小于3之间的整数
     8  
     9 print(random.choice([1,'23',[4,5]]))#1或者23或者[4,5]
    10  
    11 print(random.sample([1,'23',[4,5]],2))#列表元素任意2个组合
    12  
    13 print(random.uniform(1,3))#大于1小于3的小数,如1.927109612082716 
    14  
    15  
    16 item=[1,3,5,7,9]
    17 random.shuffle(item) #打乱item的顺序,相当于"洗牌"
    18 print(item)
    View Code
    import random
    
    def v_code():
    
        code = ''
        for i in range(5):
    
            num=random.randint(0,9)
            alf=chr(random.randint(65,90))
            add=random.choice([num,alf])
            code += str(add)
        return code
    
    print(v_code())
    4位随机验证码
    chr()根据整数返回对应的字符,也就是讲ascii转换为字符
    
    unichr()将整数返回成unicode字符
    
    ord()将字符转换成ascii码
  • 相关阅读:
    2-1(续)
    2-1
    28-69. Sqrt(x)
    27-75. Sort Colors
    26-78. Subsets
    MySQL--->存储引擎及图形化工具
    MySQL--->高级对象
    MySQL--->常用函数
    MySQL--->多表查询
    MySQL--->高级查询
  • 原文地址:https://www.cnblogs.com/zjltt/p/6957648.html
Copyright © 2011-2022 走看看