zoukankan      html  css  js  c++  java
  • day22 ramdom 模块

     1 import random
     2 
     3 #随机整数
     4 random.randint(1,5)          # 大于等于1且小于等于5之间的整数
     5 random.randrange(1,10,2)     # 大于等于1且小于10之间的奇数
     6 
     7 #随机选择一个返回
     8 random.choice([1,'23',[4,5]])  # 1或者23或者[4,5]
     9 
    10 #随机选择多个返回,返回的个数为函数的第二个参数
    11 random.sample([1,'23',[4,5]],2) # 列表元素任意2个组合
    12 
    13 #打乱列表顺序
    14 item=[1,3,5,7,9]
    15 random.shuffle(item) # 打乱次序

     1 # 随机生成验证码
     2 import random
     3 def v_code():
     4 
     5     code = ""
     6     for i in range(5):
     7         num = random.randint(0,9)
     8         alf = chr(random.randint(65,90))
     9         add    = random.choice([num,alf])
    10         code = "".join([code,str(add)])
    11     return code
    12 print(v_code())
  • 相关阅读:
    Tomcat安装与配置
    模板方法模式
    观察者模式
    访问者模式
    策略模式
    迭代器模式
    状态模式
    访问者模式
    备忘录模式
    解释器模式
  • 原文地址:https://www.cnblogs.com/shijieli/p/9909269.html
Copyright © 2011-2022 走看看