numpy.random.RandomState.random_sample

方法

random.RandomState.random_sample(size=None)

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

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

(b - a) * random_sample() + a

注解

新代码应该使用 random A方法 default_rng() 请参阅 快速启动 .

参数
sizeint或int的元组,可选

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

返回
out浮点数或浮点数

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

参见

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]])