numpy.random.random_sample#
- random.random_sample(size=None)#
返回半开区间 [0.0, 1.0) 中的随机浮点数.
Results are from the “continuous uniform” distribution over the stated interval. To sample \(Unif[a, b), b > a\) multiply the output of
random_sampleby (b-a) and add a:(b - a) * random_sample() + a
- 参数:
- sizeint 或 int 元组,可选
输出形状. 如果给定形状,例如
(m, n, k),则抽取m * n * k个样本. 默认为 None,在这种情况下,返回单个值.
- 返回:
- outfloat 或浮点数的 ndarray
形状为 size 的随机浮点数数组(除非
size=None,在这种情况下返回单个浮点数).
参见
random.Generator.random新代码应该使用它.
示例
>>> np.random.random_sample() 0.47108547995356098 # random >>> type(np.random.random_sample()) <class 'float'> >>> np.random.random_sample((5,)) array([ 0.30220482, 0.86820401, 0.1654503 , 0.11659149, 0.54323428]) # random
来自 [-5, 0) 的随机数的三行两列数组:
>>> 5 * np.random.random_sample((3, 2)) - 5 array([[-3.99149989, -0.52338984], # random [-2.99091858, -0.79479508], [-1.23204345, -1.75224494]])