1.np.random.rand(d0,d1,…,dn)
产生给定形状的随机数,随机数服从连续均匀分布分布,[0,1),返回数组
import numpy as np
np.random.rand(3,5)
Out[2]:
array([[0.30517408, 0.12289562, 0.80786515, 0.23772459, 0.95179541],
[0.25548752, 0.51234021, 0.40288592, 0.95196682, 0.07101121],
[0.43220398, 0.36122976, 0.61256135, 0.64678643, 0.80060565]])
2.np.random.random(size=None)
产生[0.0,1.0)之间的随机数(浮点数),size为整数元组或者整数,随机数服从连续均匀分布,返回数组
np.random.random((5,6))
Out[4]:
array([[0.92220195, 0.12910453, 0.42338875, 0.65139385, 0.45925132,
0.82157651],
[0.61927056, 0.39748858, 0.59825661, 0.32246338, 0.31185358,
0.49901212],
[0.49562914, 0.03043924, 0.66857092, 0.19870428, 0.58954618,
0.00641072],
[0.53382788, 0.89106008, 0.63441265, 0.2082099 , 0.22112259,
0.69459952],
[0.6591954 , 0.85651823, 0.13520124, 0.00553024, 0.00466343,
0.28531857]])
3.np.random.random_sample(size=None)
用法同np.random.random_sample(size)
4.np.random.uniform(low=0.0,high=1.0,size=None)
产生服从连续均匀分布的样本,返回随机数处于[low.high)之间,并服从均匀分布
s=np.random.uniform(-2,2,1500)
import matplotlib.pyplot as plt
plt.hist(s)
Out[8]:
(array([141., 154., 176., 154., 145., 129., 143., 150., 157., 151.]),
array([-1.99890829e+00, -1.59910013e+00, -1.19929196e+00, -7.99483800e-01,
-3.99675635e-01, 1.32529531e-04, 3.99940694e-01, 7.99748859e-01,
1.19955702e+00, 1.59936519e+00, 1.99917335e+00]),
<a list of 10 Patch objects>)

5.np.random.randint(low,high=None,size=None,dtype=‘I’)
返回[low,high)之间的随机整数,服从离散均匀分布,如果没有输入high的话,返回[0,low)之间的随机整数
dtype:可取int或int64等
s1=np.random.randint(3,size=10)
s1
Out[10]: array([2, 2, 0, 2, 0, 2, 1, 0, 0, 1])
s1=np.random.randint(10,size=1000)
plt.hist(s1)
Out[13]:
(array([ 96., 89., 103., 101., 82., 99., 108., 99., 103., 120.]),
array([0. , 0.9, 1.8, 2.7, 3.6, 4.5, 5.4, 6.3, 7.2, 8.1, 9. ]),
<a list of 10 Patch objects>)