• 随机数模块random_python


    一、随机数模块random

     1、常用的几个方法:

    import  random
    print(random.random()) #(0,1)之间的随机数字,如0.6772275352932792
    print(random.uniform(1,3)) #自定义范围内进行取值
    print(random.randint(2,89)) #两个参数以及之间的随机数
    print(random.choice('hello')) #从参数里面随机 h,可传入序列列表元组等
    print(random.sample([1,2,3,4],3)) #在序列里随机取指定个数,如[2, 1, 3]
    list=[1,2,3,4]
    print(random.shuffle(list)) #对列表随机洗牌,直接改变列表,返回空
    print(list) #[3, 4, 2, 1]
    print(random.randrange(1,5)) #在范围内随机一个整数,不包括5


    2、使用例子:随机生成5位的验证码
    随机生成5位数字:
    def v_code():
    code=''
    for i in range(0,5):
    add_num=random.randrange(10)
    code+=str(add_num)
    print(code)
    v_code()

    随机生成5位字母或数字:
    def v_code():
    code=''
    for i in range(5):
    add=random.choice([random.randrange(10),chr(random.randrange(65,91))]) #包括0-9,大写字母A-Z,随机5个人数字
    code+=str(add)
    print(code)
    字符和数字概率一样:
    import random
    def code():
    code_str=''
    for i in range(0,5):
    flag = random.randint(0, 1)
    if flag==1:
    print(chr(random.randint(65,90)))
    code_one=str(chr(random.randint(65,90)))
    else:
    code_one=str(random.randint(0,9))
    code_str = code_str+code_one
    print(code_str)

    code()
  • 相关阅读:
    mouseenter和mouseleave,mouseover和mouseout
    哈哈哈
    instanceof与typeof
    js事件传参
    浮动与清除问题
    简易富文本编辑
    js之prototype
    json序列化
    我对Defer and Promise的实现
    Ajax 完整教程
  • 原文地址:https://www.cnblogs.com/chenxiaozan/p/12154384.html
Copyright © 2020-2023  润新知