Python 生成一个范围之间的随机浮点数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22071987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generate random array of floats between a range
提问by Gabriel
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 samplingbut no function seems to do what I need.
我看过随机抽样,但似乎没有任何功能可以满足我的需求。
random.uniformcomes close but it only returns a single element, not a specific number.
random.uniform接近但它只返回一个元素,而不是一个特定的数字。
This is what I'm after:
这就是我所追求的:
ran_floats = some_function(low=0.5, high=13.3, size=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].
这将返回一个由 50 个随机非唯一浮点数组成的数组(即:允许重复),它们在范围内均匀分布[0.5, 13.3]。
Is there such a function?
有这样的功能吗?
采纳答案by JoshAdel
np.random.uniformfits your use case:
np.random.uniform适合您的用例:
sampl = np.random.uniform(low=0.5, high=13.3, size=(50,))
Update Oct 2019:
2019 年 10 月更新:
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
虽然语法仍受支持,但看起来 API 已随 NumPy 1.17 更改,以支持对随机数生成器的更大控制。展望未来,API 已更改,您应该查看https://docs.scipy.org/doc/numpy/reference/random/generated/numpy.random.Generator.uniform.html
The enhancement proposal is here: https://numpy.org/neps/nep-0019-rng-policy.html
回答by isedev
回答by pkacprzak
Why not to combine random.uniformwith a list comprehension?
为什么不将random.uniform与列表理解结合起来?
>>> def random_floats(low, high, size):
... return [random.uniform(low, high) for _ in xrange(size)]
...
>>> random_floats(0.5, 2.8, 5)
[2.366910411506704, 1.878800401620107, 1.0145196974227986, 2.332600336488709, 1.945869474662082]
回答by PhilMacKay
There may already be a function to do what you're looking for, but I don't know about it (yet?). In the meantime, I would suggess using:
可能已经有一个函数可以做你正在寻找的东西,但我不知道(还没有?)。同时,我建议使用:
ran_floats = numpy.random.rand(50) * (13.3-0.5) + 0.5
This will produce an array of shape (50,) with a uniform distribution between 0.5 and 13.3.
这将产生一个形状为 (50,) 的数组,其均匀分布在 0.5 和 13.3 之间。
You could also define a function:
您还可以定义一个函数:
def random_uniform_range(shape=[1,],low=0,high=1):
"""
Random uniform range
Produces a random uniform distribution of specified shape, with arbitrary max and
min values. Default shape is [1], and default range is [0,1].
"""
return numpy.random.rand(shape) * (high - min) + min
EDIT: Hmm, yeah, so I missed it, there is numpy.random.uniform() with the same exact call you want!
Try import numpy; help(numpy.random.uniform)for more information.
编辑:嗯,是的,所以我错过了,有 numpy.random.uniform() 与您想要的完全相同的调用!尝试import numpy; help(numpy.random.uniform)获取更多信息。
回答by George Gee
This is the simplest way
这是最简单的方法
np.random.uniform(start,stop,(rows,columns))
回答by Mohamed Ibrahim
The for loop in list comprehension takes time and makes it slow. It is better to use numpy parameters (low, high, size, ..etc)
列表理解中的 for 循环需要时间并使其变慢。最好使用 numpy 参数(低、高、大小、..等)
import numpy as np
import time
rang = 10000
tic = time.time()
for i in range(rang):
sampl = np.random.uniform(low=0, high=2, size=(182))
print("it took: ", time.time() - tic)
tic = time.time()
for i in range(rang):
ran_floats = [np.random.uniform(0,2) for _ in range(182)]
print("it took: ", time.time() - tic)
sample output:
示例输出:
('it took: ', 0.06406784057617188)
('花了:',0.06406784057617188)
('it took: ', 1.7253198623657227)
('它花了:',1.7253198623657227)
回答by Stuart Hallows
回答by shivaraj karki
np.random.random_sample(size)will generate random floats in the half-open interval [0.0, 1.0).
np.random.random_sample(size)将在半开区间 [0.0, 1.0) 中生成随机浮点数。

