简介

所谓生成随机数,即按照某种概率分布,从给定的区间内随机选取一个数。常用的分布有:均匀分布(uniform distribution),正态分布(normal distribution),泊松分布(poisson distribution)等。

python中的numpy.random模块提供了常用的随机数生成方法,下面简要总结。

按均匀分布生成随机数

rand

功能 按照均匀分布,在[0,1)内生成随机数。

接口

Docstring:

rand(d0, d1, ..., dn)

Random values in a given shape.

Create an array of the given shape and populate it with

random samples from a uniform distribution

over ``[0, 1)``.

实例

In [3]: np.random.rand()

Out[3]: 0.08621180209775481

In [4]: np.random.rand(5)

Out[4]: array([0.85086433, 0.46726857, 0.48144885, 0.36369815, 0.9181539 ])

In [5]: np.random.rand(2,5)

Out[5]:

array([[0.70784333, 0.08966827, 0.50090152, 0.22517888, 0.86735194],

[0.92224362, 0.98468415, 0.19134583, 0.06757754, 0.19330571]])

验证下是按均匀分布生成的随机数 方法:利用直方图可验证其概率密度函数是否与均匀分布一致。

x = np.random.rand(10000)

plt.hist(x,bins=20,density=True)

结果:符合均匀分布的概率密度函数,即P(x)=1, x=[0,1]

uniform

功能 按照均匀分布,在[low,high)内生成随机数。

接口

Docstring:

uniform(low=0.0, high=1.0, size=None)

Draw samples from a uniform distribution.

Samples are uniformly distributed over the half-open interval

``[low, high)`` (includes low, but excludes high).  In other words,

any value within the given interval is equally likely to be drawn

by `uniform`.

实例

In [8]: np.random.uniform(-1,1)

Out[8]: -0.34092928027362945

In [9]: np.random.uniform(-1,1,5)

Out[9]: array([ 0.84319637, -0.00449745, -0.21958133, -0.16755161, -0.94383758])

In [10]: np.random.uniform(-1,1,(2,5))

Out[10]:

array([[-0.66176972,  0.71703011,  0.61658043,  0.0373402 ,  0.95824383],

[ 0.31743693,  0.48948276,  0.0393428 ,  0.64896449,  0.3659116 ]])

验证下是按均匀分布生成的随机数

x = np.random.uniform(-1,1,10000)

plt.hist(x,bins=20,density=True)

结果:与均匀分布一致。

randint

功能 按照离散均匀分布,在[low,high)内生成随机整数。

接口

Docstring:

randint(low, high=None, size=None, dtype='l')

Return random integers from `low` (inclusive) to `high` (exclusive).

Return random integers from the "discrete uniform" distribution of

the specified dtype in the "half-open" interval [`low`, `high`). If

`high` is None (the default), then results are from [0, `low`).

实例

In [12]: np.random.randint(0,101)

Out[12]: 27

In [13]: np.random.randint(0,101,5)

Out[13]: array([71, 31, 22, 85, 64])

In [14]: np.random.randint(0,101,(2,5))

Out[14]:

array([[94, 29, 80, 40,  6],

[17, 19, 85, 11, 48]])

验证下是按均匀分布生成的随机数

x = np.random.randint(0,100,10000)

plt.hist(x,bins=20,density=True)

结果:与均匀分布一致。  注释:当区间为[0,101),即x = np.random.randint(0,100,10000)时,最后一个区间的概率密度总是会较大些。猜测跟随机数生成机制有关,暂不深入,后续有时间研究下。

按正态分布生成随机数

randn

功能 按照正态分布(

μ

=

0

,

σ

=

1

\mu=0,\sigma=1

μ=0,σ=1),生成随机数

接口

Docstring:

randn(d0, d1, ..., dn)

Return a sample (or samples) from the "standard normal" distribution.

If positive, int_like or int-convertible arguments are provided,

`randn` generates an array of shape ``(d0, d1, ..., dn)``, filled

with random floats sampled from a univariate "normal" (Gaussian)

distribution of mean 0 and variance 1 (if any of the :math:`d_i` are

floats, they are first converted to integers by truncation). A single

float randomly sampled from the distribution is returned if no

argument is provided.

This is a convenience function.  If you want an interface that takes a

tuple as the first argument, use `numpy.random.standard_normal` instead.

实例

In [15]: np.random.randn()

Out[15]: 0.2594770528010187

In [16]: np.random.randn(5)

Out[16]: array([ 0.51858431, -0.56406054,  0.39934797,  0.87223161,  0.56642685])

In [17]: np.random.randn(2,5)

Out[17]:

array([[-0.09649912, -0.51282169,  0.10047756,  1.03372611, -1.54014928],

[-1.29894642,  0.46580577,  0.77321341,  2.16154836, -0.99208604]])

验证下是按正态分布生成的随机数

x = np.random.randn(10000)

n, bins, patches =plt.hist(x,bins=50,density=True)

y1 = scipy.stats.norm.pdf(bins, loc=0, scale=1)

plt.plot(bins,y1,'r--')

结果:符合正态分布。

normal

功能 按照正态分布(

μ

,

σ

\mu,\sigma

μ,σ),生成随机数

接口

Docstring:

normal(loc=0.0, scale=1.0, size=None)

Draw random samples from a normal (Gaussian) distribution.

实例

In [22]: np.random.normal(170,10)

Out[22]: 169.24973434084185

In [23]: np.random.normal(170,10,5)

Out[23]:

array([167.0171718 , 169.47192942, 169.61260805, 151.53654937,

172.61541579])

In [24]: np.random.normal(170,10,(2,5))

Out[24]:

array([[187.28916343, 170.30553783, 175.69009811, 171.87050032,

179.26404146],

[171.17078449, 173.25610155, 169.60757285, 178.79427632,

163.25814631]])

验证下是按正态分布生成的随机数

mu,sigma = 170,10

x = np.random.normal(mu,sigma,10000)

n, bins, patches = plt.hist(x,bins=50,density=True)

y1 = scipy.stats.norm.pdf(bins, loc=mu, scale=sigma)

plt.plot(bins,y1,'r--')

结果:符合正态分布

给定一个数组,随机排列

permutation

功能 给定一个数组(或list,随机排列

接口

Randomly permute a sequence, or return a permuted range.

If `x` is a multi-dimensional array, it is only shuffled along its

first index.

Parameters

----------

x : int or array_like

If `x` is an integer, randomly permute ``np.arange(x)``.

If `x` is an array, make a copy and shuffle the elements

randomly.

Returns

-------

out : ndarray

Permuted sequence or array range.

实例

In [28]: np.random.permutation(6)

Out[28]: array([2, 1, 3, 4, 0, 5])

In [29]: np.random.permutation(np.arange(1,7))

Out[29]: array([5, 3, 4, 2, 1, 6])

shuffle

功能 给定一个数组或list,打乱顺序(同permutation)

接口

Docstring:

shuffle(x)

Modify a sequence in-place by shuffling its contents.

This function only shuffles the array along the first axis of a

multi-dimensional array. The order of sub-arrays is changed but

their contents remains the same.

Parameters

----------

x : array_like

The array or list to be shuffled.

Returns

-------

None

实例

In [32]: x = np.arange(1,7)

In [33]: x

Out[33]: array([1, 2, 3, 4, 5, 6])

In [34]: np.random.shuffle(x)

In [35]: x

Out[35]: array([4, 1, 6, 3, 5, 2])

自定义概率分布抽样

choice

功能 给定一个数组或list,从中抽样。可自定义概率分布,即自定义各元素被抽到的概率(默认为均匀分布)

接口

Docstring:

choice(a, size=None, replace=True, p=None)

Generates a random sample from a given 1-D array

注意:replace为True,则抽样出的n个元素可以重复,即每次抽样出一个元素后,还会将其放回样本池,默认可重复;反之。

实例 1、元素为均匀分布

In [107]: x = np.arange(1,7)

In [108]: np.random.choice(x,5)

Out[108]: array([3, 4, 5, 6, 3])

In [109]: np.random.choice(x,5,replace=False)

Out[109]: array([2, 1, 5, 6, 4])

2、自定义元素的概率分布

In [110]: x = ['石头','剪刀','布']

In [111]: np.random.choice(x,5,p=[0.2,0.3,0.5])

Out[111]: array(['布', '布', '布', '布', '剪刀'], dtype='

同理,可验证是否按照自定义的概率分布进行抽样。

x = ['石头','剪刀','布']

res = np.random.choice(x,5000,p=[0.2,0.3,0.5])

_, ax = plt.subplots()

n,bins,patches = ax.hist(res,bins=3,density=1)

p = n*(bins[1]-bins[0])

s = [ax.get_xticklabels()[i].get_text() for i in range(3)]

print(list(zip(s,p)))

结果:

[('石头', 0.19440000000000002), ('布', 0.5024), ('剪刀', 0.3031999999999999)]

总结

用uniform(low, high, size)生成[low, high)内均匀分布的随机数(float) low,high默认为[0,1),即等价于rand()用randint(low, high, size)生成[low, high)内均匀分布的随机整数(int)用normal(loc, scale,size)随机生成正态分布(

μ

,

σ

\mu,\sigma

μ,σ)的随机数(float) loc,scale默认为0,1,即等价于randn()用choice生成自定义概率分布的随机数(或其他类型元素)

python自定义随机数_python:numpy.random模块生成随机数相关推荐

  1. python random库生成伯努利随机数的方法_Python使用random模块生成随机数操作实例详解...

    本文实例讲述了Python使用random模块生成随机数操作.分享给大家供大家参考,具体如下: 今天在用Python编写一个小程序时,要用到随机数,于是就在网上查了一下关于Python生成各种随机数的 ...

  2. python中产生随机数模块_Python中random模块生成随机数详解

    Python中的random模块用于生成随机数.下面介绍一下random模块中最常用的几个函数. random.random random.random()用于生成一个0到1的随机符点数: 0 < ...

  3. python中sn的意思_Python中random模块生成随机数详解

    Python中的random模块用于生成随机数.下面介绍一下random模块中最常用的几个函数. random.random random.random()用于生成一个0到1的随机符点数: 0 < ...

  4. python中random模块中包含了随机数相关的功能函数_Python中random模块生成随机数详解...

    print random.randint(12, 20) #生成的随机数n: 12 <= n <= 20 print random.randint(20, 20) #结果永远是20 #pr ...

  5. python生成50个随机数_Python内置random模块生成随机数的方法

    本文我们详细地介绍下两个模块关于生成随机序列的其他使用方法. 随机数参与的应用场景大家一定不会陌生,比如密码加盐时会在原密码上关联一串随机数,蒙特卡洛算法会通过随机数采样等等.Python内置的ran ...

  6. python中的random模块_Python内置random模块生成随机数的方法

    本文我们详细地介绍下两个模块关于生成随机序列的其他使用方法. 随机数参与的应用场景大家一定不会陌生,比如密码加盐时会在原密码上关联一串随机数,蒙特卡洛算法会通过随机数采样等等.Python内置的ran ...

  7. python产生随机数random.random_Python内置random模块生成随机数的方法

    本文我们详细地介绍下两个模块关于生成随机序列的其他使用方法. 随机数参与的应用场景大家一定不会陌生,比如密码加盐时会在原密码上关联一串随机数,蒙特卡洛算法会通过随机数采样等等.Python内置的ran ...

  8. Python中random模块生成随机数详解

    Python中random模块生成随机数详解 本文给大家汇总了一下在Python中random模块中最常用的生成随机数的方法,有需要的小伙伴可以参考下 Python中的random模块用于生成随机数. ...

  9. Python中如何用random模块生成随机数并重现

    使用random模块生成随机数 Python广为人们喜爱的原因就是Python中有许许多多非常便捷的模块可供我们随意调用,在Python中我们可以通过调用random模块来生成一个伪随机数. --只有 ...

最新文章

  1. C# 篇基础知识11——泛型和集合
  2. linux mac中实现类似secureCRT的clone session
  3. c command语言学例子,语言学第四章
  4. list转字符串_剑指offer 38——字符串的排列
  5. wifi情况下使用fiddler_钢筋网片在什么情况下使用?
  6. 2016-02-24 获取设备 通知开关
  7. Data Pump failed with ORA-04031/ORA-4030?
  8. 看看一个朋友写的代码,大家发表发表意见,比较简单的代码
  9. 类数组变量定义与初始化
  10. csharp基础练习题:卡塔劳尔【难度:1级】--景越C#经典编程题库,不同难度C#练习题,适合自学C#的新手进阶训练
  11. Colibri 片段化学空间的兴起
  12. 搜狐全体员工遭遇工资补助诈骗,CEO张朝阳回应:没那么严重
  13. 小米手机电池耗尽后进入fastboot死循环的退出方法
  14. CFA一级学习笔记--固定收益(一)--基本概念
  15. 做跨境电商的Anker的也回来“内卷”了?
  16. 数据结构与算法学习笔记(五)树
  17. lab值意义_lab是什么意思?
  18. 移动互联网下半场的面试真经,让你进入 BAT 不再是梦
  19. 拼多多双滑块识别/拼多多空间点选/验证码本地库识别
  20. 翻译-linux-5.1.2\Documentation\virtual\kvm\api.txt

热门文章

  1. 避开移入移出事件内部div干扰事件,e是function(e)的e
  2. vim 7.4同时支持python 2.x和3.x问题调研
  3. .net core在vs开发环境下脱离iis运行
  4. Https的底层原理
  5. 认识和了解python
  6. 报表性能优化方案之报表服务器优化基础讲解
  7. Animation Override Controller动画重载器
  8. IE6、IE7、IE8的CSS、JS兼容
  9. MATLAB【十四】————调用深度库生成exe,批量运行三层文件夹下图片,保存结果
  10. C#中在应用程序和DLL使用消息