在范围内生成随机浮点数数组

166

我还没有找到一个能够在给定范围内生成指定长度的随机浮点数数组的函数。

我查看了随机抽样,但是没有一个函数似乎可以达到我的需求。

random.uniform接近我的要求,但它只返回单个元素而不是特定数量。

这就是我想要的:

ran_floats = some_function(low=0.5, high=13.3, size=50)

是否有这样的函数?会返回一个由50个随机非唯一浮点数组成的数组(即:允许重复),均匀分布在范围[0.5, 13.3]内。

这样的函数存在吗?


5
你在问题中标记了 numpy ,但你没有提到 numpy.random.uniform,尽管它恰好有你想要的调用签名。你是否已经导入了 numpy 库? - DSM
2
[random.uniform(low, high) for i in xrange(size)] - Phylogenesis
1
@DSM 是的,我看到了,你显然是100%正确的。我错过了那个函数,它似乎正好做我需要的事情。你介意把你的评论作为答案呈现吗? - Gabriel
10个回答

242

36
OP的直观搜索问题是 some_function(low=0.5, high=13.3, size=50)。这展示了Python库的出色设计。#厉害 - Saravanabalagi Ramachandran
大小不是很清楚,链接也无效。这里是一些细微的澄清。size:int或int元组,可选。输出形状。 如果给定的形状为(m,n,k),则会绘制m * n * k个样本。 如果大小为None(默认),则如果低值和高值都是标量,则返回单个值。 - vlad
@vlad - 感谢您指出链接的问题。我已经更新了答案,希望能够涵盖当前的使用情况。 - JoshAdel
在Numpy的官方文档中,以下函数解决了该问题。 https://numpy.org/doc/stable/reference/random/generated/numpy.random.random_sample.html - Loich

30

为什么不使用列表推导式?

在Python 2中

ran_floats = [random.uniform(low,high) for _ in xrange(size)]
在Python 3中,range 的工作方式类似于 xrange(ref)。
ran_floats = [random.uniform(low,high) for _ in range(size)]

11

这是最简单的方法

np.random.uniform(start,stop,(rows,columns))

7

可能已经有一个可以实现你想要的功能的函数,但我还不知道(现在?)。 同时,我建议使用:

ran_floats = numpy.random.rand(50) * (13.3-0.5) + 0.5

这将生成一个形状为(50,)的数组,在0.5和13.3之间具有均匀分布。
您还可以定义一个函数:
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

编辑:嗯,我错过了,有一个与您想要的完全相同的调用numpy.random.uniform()! 尝试import numpy; help(numpy.random.uniform)以获取更多信息。


6

Alternatively you could use SciPy

from scipy import stats
stats.uniform(0.5, 13.3).rvs(50)

为了采样整数,可以使用以下代码:

randint()


stats.randint(10, 20).rvs(50)

4

为什么不把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]

4

使用列表推导式中的for循环会消耗时间,使程序变慢。更好的方法是使用numpy参数(如low、high、size等)。

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)

示例输出:

('花费时间:', 0.06406784057617188)

('花费时间:', 1.7253198623657227)


1

或者,如果您可以接受一个实数列表,您可以使用标准的 random.randrange

def some_function(low, high, size):
    low_int = int(low * 1000)
    high_int = int(high *1000)
    return [random.randrange(low_int, high_int, size)/1000 for _ in range(size)]

0

这应该适用于你的示例

sample = (np.random.random([50, ]) * 13.3) - 0.5

0

np.random.random_sample(size) 会在半开区间 [0.0, 1.0) 中生成随机浮点数。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接