上一个主题

numpy.random.ranf

下一个主题

numpy.random.choice

numpy.random.sample

numpy.random.sample(size=None)

返回半开区间内的随机浮点数[0.0,1.0]。

结果来自于规定间隔内的“连续均匀”分布。样本 Unif[a, b), b > a 乘以的输出 random_sample 通过 (b-a) 并添加 a ::

(b - a) * random_sample() + a
参数:
size : int或int的元组,可选

输出形状。如果给定的形状是,例如, (m, n, k) 然后 m * n * k 取样。默认值为无,在这种情况下返回单个值。

返回:
out : 浮点数或浮点数

形状随机浮动数组 size (除非 size=None ,在这种情况下返回单个浮点)。

实例

>>> np.random.random_sample()
0.47108547995356098
>>> type(np.random.random_sample())
<type 'float'>
>>> np.random.random_sample((5,))
array([ 0.30220482,  0.86820401,  0.1654503 ,  0.11659149,  0.54323428])

从[-5,0]开始的三乘二随机数数组:

>>> 5 * np.random.random_sample((3, 2)) - 5
array([[-3.99149989, -0.52338984],
       [-2.99091858, -0.79479508],
       [-1.23204345, -1.75224494]])