numpy.random中的函数
rand()
生成随机值
>>>np.random.rand(3)
array([0.68256392, 0.14294158, 0.72120024])
randn()
生成标准正态分布样本
>>>np.random.randn(3)
array([-2.49001983, -0.33634447, -0.57466392])
randint(low,high,size)
生成左闭右开的随机整数
randint(high[,size]) 取小于high的整数[个数为size]
>>>np.random.randint(2,10,3)
array([9, 7, 4])
random_integers(low,high,size)
左闭右闭的随机整数,注意,python3.8.1已经不再使用这个函数
>>>np.random.random_integers(4,size=10)
Warning (from warnings module):
File "<pyshell#11>", line 1
DeprecationWarning: This function is deprecated. Please call randint(1, 4 + 1) instead
array([3, 1, 3, 4, 2, 1, 4, 4, 1, 4])
random_sample([size])、random([size]) 、ranf([size])、sample([size])
生成[0.0,1.0)间的随机浮点数
>>>np.random.random_sample(3)
array([0.07828906, 0.92357492, 0.23780065])
>>>np.random.random(3)
array([0.92554691, 0.98998066, 0.1320273 ])
>>>np.random.ranf(3)
array([0.57944383, 0.89816435, 0.8438508 ])
>>>np.random.sample(3)
array([0.01619052, 0.04228103, 0.89007802])
choice(a[, size, replace, p])
从一个给定的数组a中生成随机样本,其中p为a中元素被抽取的概率分布
>>>np.random.choice([2,3,1,4,8,5],10,p=[0.3,0.5,0,0,0.1,0.1])
array([3, 3, 3, 3, 2, 3, 8, 3, 5, 3])
uniform(low,high,size)
生成 [low.high) 间的均匀分布,注意是左闭右开
>>>np.random.uniform(-1,1,3)
array([-0.52535198, -0.81110613, -0.69339663])