首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python 标准库:random

python 标准库:random

作者头像
我是一条小青蛇
发布2019-10-23 13:11:41
7830
发布2019-10-23 13:11:41
举报
文章被收录于专栏:青笔原创青笔原创

在数据分析,数据清洗,数据集处理中,除了使用,我们熟悉的 numpy.random 模块来生成随机数,或者随机采样,事实上,python 标准库也提供了 random 模块,如果不想,仅仅因为使用随机数,而单独导入 numpy 时,标准库提供的 random 模块,不失为一种,轻量级替代方案,并且两者使用起来几乎一样。

1. 导入模块

random 是 python 标准库模块,随 python 一起安装,无需单独安装,可直接导入。

import random

2. 实用方法

2.1 random()

random()生成一个位于半开放区间 [0, 1) 的浮点数,几乎所有random模块的方法的实现,都依赖于 random()

random.random()
0.9111245252327139

2.2 randint(a, b)

返回随机整数 N 满足 a <= N <= b。相当于 randrange(a, b+1)。

random.randint(0, 10)
8

2.3 randrange(start, stop[, step])

range(start, stop, step) 返回一个随机选择的元素。 这相当于 choice(range(start, stop, step)) ,但实际上并没有构建一个 range 对象。

random.randrange(0, 10, 2)
2

2.4 choice(seq)

从非空序列 seq 返回一个随机元素。 如果 seq 为空,则引发 IndexError。

random.choice([0, 1, 2, 3, 4, 5])
2

2.5 choices(population, weights=None, *, cum_weights=None, k=1)

population中选择替换,返回大小为 k 的元素列表。 如果 population 为空,则引发 IndexError。

如果指定了 weight 序列,则根据相对权重进行选择。 或者,如果给出 cum_weights 序列,则根据累积权重(可能使用 itertools.accumulate() 计算)进行选择。 例如,相对权重[10, 5, 30, 5]相当于累积权重[10, 15, 45, 50]。 在内部,相对权重在进行选择之前会转换为累积权重,因此提供累积权重可以节省工作量。

random.choices([0, 1, 2, 3, 4, 5], k=2)
[0, 2]
random.choices([0, 1, 2, 3, 4, 5], cum_weights=[10, 20, 30, 40, 50, 60], k=3)
[4, 4, 4]

2.6 shuffle(x[, random])

将序列 x 随机打乱位置。

可选参数 random 是一个0参数函数,在 [0.0, 1.0) 中返回随机浮点数;默认情况下,这是函数 random() 。

要改变一个不可变的序列并返回一个新的打乱列表,请使用sample(x, k=len(x))

a = [0, 1, 2, 3, 4, 5]
random.shuffle(a)
a
[5, 2, 3, 0, 1, 4]

2.7 sample(population, k)

返回从总体序列或集合中选择的唯一元素的 k 长度列表。 用于无重复的随机抽样。

random.sample([1, 2, 3, 4, 5, 6], k=2)
[1, 3]

2.8 gauss(mu, sigma)

高斯分布。 mu 是平均值,sigma 是标准差。

[random.gauss(0, 1) for i in range(10)]
[1.232295558291998,
 -0.23589397085653746,
 -1.4190307151921895,
 0.18999908858301912,
 0.780671045104774,
 0.041722424850158674,
 0.7392269754813698,
 1.4612049925568829,
 0.09647538110312114,
 -0.32525720572670025]

3. 源码简要

以下为 python 官方 github 上,random 模块的部分源码,帮助了解 random 模块的基本结构,以及本文介绍的实用方法的源码申明。

random.py:

class Random(_random.Random):
    def seed(self, a=None, version=2):
        pass
    def randrange(self, start, stop=None, step=1, _int=int):
        pass
    def randint(self, a, b):
        pass
    def choice(self, seq):
        pass
    def shuffle(self, x, random=None):
        pass
    def sample(self, population, k):
        pass
    def choices(self, population, weights=None, *, cum_weights=None, k=1):
        pass
    def gauss(self, mu, sigma):
        pass
    ...

_inst = Random()
seed = _inst.seed
random = _inst.random
randrange = _inst.randrange
randint = _inst.randint
choice = _inst.choice
shuffle = _inst.shuffle
sample = _inst.sample
choices = _inst.choices
gauss = _inst.gauss

...

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-052,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 导入模块
  • 2. 实用方法
    • 2.1 random()
      • 2.2 randint(a, b)
        • 2.3 randrange(start, stop[, step])
          • 2.4 choice(seq)
            • 2.5 choices(population, weights=None, *, cum_weights=None, k=1)
              • 2.6 shuffle(x[, random])
                • 2.7 sample(population, k)
                  • 2.8 gauss(mu, sigma)
                  • 3. 源码简要
                  领券
                  问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档