• 技术文章 >Python技术 >Python基础教程

    Python之random模块详解

    爱喝马黛茶的安东尼爱喝马黛茶的安东尼2019-07-02 13:24:58原创3573

    python的random模块

    random模块是python中一个生成随机数的模块。

    random不是python解释器内置的模块。

    导入random模块的方法是:

    1

    import random

    如果只使用random模块中的单个方法的话,也可以使用

    1

    from random import method_name

    例如:

    我只想生成一个10以内的随机的整数,不需要random模块的别的方法的时候,也可以使用以下命令

    1

    2

    from random import randint

    random.randint(0,10)

    查看random模块的内置方法可以使用以下命令:

    1

    dir(random)

    其中常用的方法有下面几个:

    choice

    1

    2

    #从一个非空列表中随机选择一个元素

    >Choose a random element from a non-empty sequence.

    1

    2

    3

    4

    >>> random.choice([1,3,5,7])

    1

    >>> random.choice([1,3,5,7])

    3

    相关推荐:《Python视频教程

    randint

    1

    2

    #从a和b(包括b)的范围内随机生成一个整数

    >Return random integer in range [a, b], including both end points.

    1

    2

    3

    4

    5

    6

    7

    8

    >>> random.randint(0,9)

    8

    >>> random.randint(0,9)

    0

    >>> random.randint(0,9)

    4

    >>> random.randint(0,9)

    3

    random

    1

    2

    #生成一个0(包括0)到1内的浮点数

    >random() -> x in the interval [0, 1).

    1

    2

    3

    4

    5

    6

    >>> random.random()

    0.3898009217264272

    >>> random.random()

    0.897328889551127

    >>> random.random()

    0.9899842422616898

    randrange

    1

    2

    3

    4

    #在指定范围内随机生成一个整数

    > Choose a random item from range(start, stop[, step]).

    This fixes the problem with randint() which includes the

    endpoint; in Python this is usually not what you want.

    1

    2

    3

    4

    5

    6

    7

    8

    >>> random.randrange(100,200)

    156

    >>> random.randrange(100,200)

    133

    >>> random.randrange(10,20)

    11

    >>> random.randrange(10,20)

    15

    sample

    1

    2

    #从一个列表或集合中随机选择多个元素

    >Chooses k unique random elements from a population sequence or set.

    1

    2

    3

    4

    >>> random.sample([23,[1,2,3],"aa","yy"],2)

    ['aa', 23]

    >>> random.sample([23,[1,2,3],"aa","yy"],3)

    ['aa', [1, 2, 3], 23]

    shuffle

    1

    2

    #把一个列表内元素的顺序打乱,列表的内存地址不变

    >Shuffle list x in place, and return None.

    1

    2

    3

    4

    5

    6

    7

    8

    >>> l1=[1,"a",3,5,"b","c"]

    >>> id(l1)

    140436582171208

    >>> random.shuffle(l1)

    >>> print(l1)

    [1, 'b', 'a', 'c', 3, 5]

    >>> id(l1)

    140436582171208

    uniform

    1

    2

        #在指定范围内随机生成一个浮点数

    >Get a random number in the range [a, b) or [a, b] depending on rounding.

    1

    2

    3

    4

    5

    6

    >>> random.uniform(12,33)

    27.02416276339153

    >>> random.uniform(12,33)

    13.832414985007832

    >>> random.uniform(12,33)

    12.827493699496461

    现在想生成一个5位包含大小写和数字的随机验证码,代码如下:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    import random

    def random_code():

        random_str = ""

        for i in range(5):

            #随机选择一个整数

            num=random.randint(0,9)

            #生成一个大写字母

            upper=chr(random.randint(65,90))

            #生成一个小写字母

            lower=chr(random.randint(97,122))

            #每次从大小写字母中随机选择一位

            res=random.choice([str(num),upper,lower])

            random_str+=res

        return random_str

    print(random_code())

    运行5次这个程序,生成的验证码如下:

    1

    2

    3

    4

    5

    KwlTN

    t1Pag

    294l6

    t1Pag

    294l6

    专题推荐:python random
    上一篇:Python之time模块详解 下一篇:Python导入模块,Python import用法(超级详细)

    相关文章推荐

    • Python random模块及用法• Python traceback模块:获取异常信息• Python之time模块详解

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网