生成范围之间的随机浮点数组 [英] Generate random array of floats between a range
问题描述
我无法找到一个函数来生成在一定范围内给定长度的随机浮点数组.
I haven't been able to find a function to generate an array of random floats of a given length between a certain range.
我查看了随机采样,但是没有任何功能似乎可以满足我的需求.
I've looked at Random sampling but no function seems to do what I need.
random.uniform 接近,但只返回一个单个元素,而不是特定的数字.
random.uniform comes close but it only returns a single element, not a specific number.
这就是我所追求的:
ran_floats = some_function(low=0.5, high=13.3, size=50)
,它将返回均匀分布在[0.5, 13.3]
范围内的50个随机非唯一浮点数(即:允许重复)的数组.
which would return an array of 50 random non-unique floats (ie: repetitions are allowed) uniformly distributed in the range [0.5, 13.3]
.
有这样的功能吗?
推荐答案
np.random.uniform
fits your use case:
sampl = np.random.uniform(low=0.5, high=13.3, size=(50,))
更新2019年10月:
虽然仍然支持语法,但是看起来API随NumPy 1.17更改了,以支持对随机数生成器的更好控制.前进的API已经更改,您应该查看
While the syntax is still supported, it looks like the API changed with NumPy 1.17 to support greater control over the random number generator. Going forward the API has changed and you should look at https://docs.scipy.org/doc/numpy/reference/random/generated/numpy.random.Generator.uniform.html
增强建议在此处: https://numpy.org/neps/nep-0019-rng-policy.html
这篇关于生成范围之间的随机浮点数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!