美文网首页
random module

random module

作者: cutelittlePanda | 来源:发表于2017-08-29 13:18 被阅读0次

    import random

    random.random()  ===> [0.0, 1.0)

    random.choice(seq): return an element from the non-empty sequence.

    print(random.choice([x for x in range(201) if x%5==0 and x%7==0]))

    random.sample(seq/set, k): randomly return a list including k items 

    print(random.sample([x for x in range(100, 201)], 5))  ===>[113, 196, 192, 180, 111]

    print(random.sample([x for x in range(1,1001) if x%5==0 and x%7==0], 5)) ==> [420, 700, 455, 525, 875]

    num = random.uniform(a, b)

    a < num < b if a < b

    b < num < a if a > b

    >>> import random

    >>> random.uniform(7, 15)

    11.072817090758864

    >>> random.uniform(7, 3)

    5.372640370008597

    >>> int(random.uniform(10, 3))

    7

    >>>

    random.shuffle(seq): the seq will be changed, and return NULL

    OR

    from random import shuffle

    shuffle(list_seq)

    the return is Null, but the sequence of list is changed randomly.

    shuffle() and random.shuffle()

    相关文章

      网友评论

          本文标题:random module

          本文链接:https://www.haomeiwen.com/subject/rwbgdxtx.html