zoukankan      html  css  js  c++  java
  • Python Random模块

    构造随机是程序中经常使用的功能,Python内置了这方面的支持。简洁又高效。这篇博客主要记录一下Random中经常使用的几个函数功能。

    random.random() :返回一个零到一之间左闭右开的浮点数。
    Return the next random floating point number in the range [0.0, 1.0).

    random.uniform(a, b) :返回a到b之间的一个浮点数。


    Return a random floating point number N such that a <= N <= b for a <= b and b <= N <= a for b < a.
    实现方式:a + (b-a) * random()

    random.randint(a, b) :返回a到b之间的整数。包含a和b。
    Return a random integer N such that a <= N <= b. Alias for randrange(a, b+1).

    random.choice(seq) : 返回序列seq中的一个随机元素。假设序列为空,则会报错。
    Return a random element from the non-empty sequence seq. If seq is empty, raises IndexError.

    random.randrange(stop) ,random.randrange(start, stop[, step]) : 等同于choice(range(start, stop, step))。

    但不会创建range对象。


    Return a randomly selected element from range(start, stop, step). This is equivalent to choice(range(start, stop, step)), but doesn’t actually build a range object.

    random.sample(population, k) : 返回一个列表。为population中前K个元素的乱序。
    Return a k length list of unique elements chosen from the population sequence or set. Used for random sampling without replacement.

    random.shuffle(x[, random]) : 本身没有返回值,作用为将x中的元素打乱。
    Shuffle the sequence x in place. The optional argument random is a 0-argument function returning a random float in [0.0, 1.0); by default, this is the function random().

  • 相关阅读:
    PDO应用
    分页查询
    PHP去除数组中重复数据的两个例子
    数据库访问(现用基本格式)
    克隆、加载类
    抽象类和接口
    静态
    PHP基础
    数据库的常用函数
    数据库的高级查询
  • 原文地址:https://www.cnblogs.com/gccbuaa/p/7241064.html
Copyright © 2011-2022 走看看