zoukankan      html  css  js  c++  java
  • python random模块

    import random

    random.uniform():生成指定范围内的随机符点数.

    >>random.uniform(1, 10)
    2.6342110774014387

    random.randint():生成一个指定范围内随机整数

    >>> random.randint(1, 10)
    2

    random.randrange():返回range()内的随机整数

    >>> random.randrange(1, 10)
    2

    或者如下:

    >>> random.randrange(1, 10, 2)
    7

    random.choice():返回一个字符串的随机字符,也可以返回一个列表中的随机元素。

    >>> random.choice("abcd!@#$%efd")
    '!'
    >>> random.choice(['a', 'b', 3, 4])
    3

    random.sample():选取指定数量的随机元素

    >>> random.sample("abcdefd", 4)
    ['e', 'b', 'a', 'f']

    random.shuffle():随机排序

    >>> card = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    >>> random.shuffle(card)
    >>> card
    [6, 9, 8, 1, 3, 2, 5, 7, 4]
    >>>
  • 相关阅读:
    POJ 2154
    POJ 1286
    Polycarp's problems
    Greedy Change
    Goods transportation
    Ugly Problem
    Happy Matt Friends
    Dense Subsequence
    Ray Tracing
    Batch Sort
  • 原文地址:https://www.cnblogs.com/guolei2570/p/8759719.html
Copyright © 2011-2022 走看看