python⾃定义随机数_python:numpy.random模块⽣成随机数简介
所谓⽣成随机数,即按照某种概率分布,从给定的区间内随机选取⼀个数。常⽤的分布有:均匀分布(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
功能 按照正态分布(
μ
=
,
σ
=
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 = 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.
实例
python生成1到100之间随机数In [22]: al(170,10)
Out[22]: 169.24973434084185
In [23]: al(170,10,5)
Out[23]:
array([167.0171718 , 169.47192942, 169.61260805, 151.53654937,
172.61541579])
In [24]: al(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 = al(mu,sigma,10000)
n, bins, patches = plt.hist(x,bins=50,density=True)
y1 = 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)
接⼝

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。