numpy.random.RandomState.random_sample#
method
- 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
- 参数:
- 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]])