目录
1.numpy.random.rand(d0, d1, ..., dn) 生成服从均匀分布[0,1)的随机数、或指定size的随机数组
2.numpy.random.randint(low, high=None, size=None, dtype=int) 生成服从离散均匀分布[low,high)的随机数、或指定size的随机数组
3. numpy.random.randn(d0, d1, ..., dn) 生成服从均值0方差1的正态分布的随机数、或指定size的随机数组
4. np.random.normal(loc=0.0, scale=1.0, size=None) 生成服从正态(高斯)分布的随机数(可自行选择均值、方差以及输出的size)
1)用法: numpy.random.rand(d0, d1, ..., dn)
2)作用:从均匀分布[0,1)中抽取样本, 生成指定形状的数组,数组里的数是服从[0,1)均匀分布的随机数
3)参数:
d0, d1, …, dn :数组的形状(The dimensions of the returned array, must be non-negative. If no argument is given a single Python float is returned.)
4)输出:
给定形状的数组 (ndarray, shape (d0, d1, ..., dn))
5)例子:随机生成一个3行2列的数组(服从[0,1)均匀分布)
#随机生成一个3行2列的数组(服从[0,1)均匀分布)
np.random.rand(3,2)
array([[ 0.14022471, 0.96360618], #random
[ 0.37601032, 0.25528411], #random
[ 0.49313049, 0.94909878]]) #random
1)用法: numpy.random.randint(low, high=None, size=None, dtype=int)
2)作用:从离散均匀分布[low,high)中抽取样本,生成一个随机整数、或者由随机数组成的数组
3)参数:
low, high (范围区间为[low,high)) ( If high is None (the default), then results are from [0, low).)
size:数组的形状(The dimensions of the returned array)
dtype(默认为int)
4)输出:给定形状的随机数组或一个随机整数
int or ndarray of ints. (size-shaped array of random integers from the appropriate distribution, or a single such random int if size not provided.)
1) 如果指定size(数组的形状),输出指定size的随机数组
2) 如果没有指定size(数组的形状),则输出一个随机数
5)例子:随机生成一个2行4列的数组(数组形状size=(2, 4),数组里的数在0~4之间)
#随机生成一个2行4列的数组(数组形状size=(2, 4),随机数在0~4之间)
Generate a 2 x 4 array of ints between 0 and 4, inclusive:
>>>
np.random.randint(5, size=(2, 4))
array([[4, 0, 2, 1], # random
[3, 2, 2, 0]])
1)用法: numpy.random.randn(d0, d1, ..., dn)
2)作用:从均值0方差1的正态分布中抽取样本,生成一个随机数、或者指定形状的随机数组
3)参数:
d0, d1, …, dn :数组的形状(The dimensions of the returned array, must be non-negative. If no argument is given a single Python float is returned.)
4)输出:
ndarray or float 【 A (d0, d1, ..., dn)
-shaped array of floating-point samples from the standard normal distribution, or a single such float if no parameters were supplied.】
给定形状的随机数组或一个随机数
5)例子:
Examples
>>>
np.random.randn()
2.1923875335537315 # random
Two-by-four array of samples from N(3, 6.25):
>>>
3 + 2.5 * np.random.randn(2, 4)
array([[-4.49401501, 4.00950034, -1.81814867, 7.29718677], # random
[ 0.39924804, 4.68456316, 4.99394529, 4.84057254]]) # random
1)用法: numpy.random.
normal
(loc=0.0, scale=1.0, size=None)
2)作用:生成服从正态(高斯)分布的随机数(可自行选择均值、方差以及输出的size)
3)参数:
loc: 均值( Mean (“centre”) of the distribution.)
scale: 标准差 (Standard deviation (spread or “width”) of the distribution. Must be non-negative.)
size: 输出的形状 (int or tuple of ints, optional)
4)输出:
ndarray or scalar 数组或标量(Drawn samples from the parameterized normal distribution.)
5)例子: 生成均值为2,标准差为2.5,2行4列的随机数
Two-by-four array of samples from N(3, 6.25):
>>>
np.random.normal(3, 2.5, size=(2, 4))
array([[-4.49401501, 4.00950034, -1.81814867, 7.29718677], # random
[ 0.39924804, 4.68456316, 4.99394529, 4.84057254]]) # random
参考:
numpy-random函数
np.random用法
Python Numpy随机数总结——numpy.random.rand/randn/randint/random/uniform/seed