在python中,random.uniform()和random.random()有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30030659/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 07:51:56  来源:igfitidea点击:

In python, what is the difference between random.uniform() and random.random()?

pythonrandomuniform

提问by NKCP

In python for the random module, what is the difference between random.uniform()and random.random()? They both generate pseudo random numbers, random.uniform()generates numbers from a uniform distribution and random.random()generates the next random number. What is the difference?

在蟒蛇的随机模块,之间有什么区别random.uniform()random.random()?它们都生成伪随机数,random.uniform()从均匀分布生成数字并random.random()生成下一个随机数。有什么不同?

回答by Bill the Lizard

The difference is in the arguments. It's very common to generate a random number from a uniform distribution in the range [0.0, 1.0), so random.random()just does this. Use random.uniform(a, b)to specify a different range.

区别在于论据。从 [0.0, 1.0) 范围内的均匀分布生成随机数是很常见的,所以random.random()就这样做。使用random.uniform(a, b)指定不同的范围。

回答by RickardSjogren

According to the documentation on random.uniform:

根据文档random.uniform

Return a random floating point number N such that a <= N <= b for a <= b and b <= N <= a for b < a.

返回一个随机浮点数 N 使得 a <= N <= b for a <= b 和 b <= N <= a for b < a。

while random.random:

同时random.random

Return the next random floating point number in the range [0.0, 1.0).

返回范围 [0.0, 1.0) 中的下一个随机浮点数。

I.e. with random.uniformyou specify a range you draw pseudo-random numbers from, e.g. between 3 and 10. With random.randomyou get a number between 0 and 1.

即与random.uniform您指定一个范围,您从中绘制伪随机数,例如在 3 和 10 之间。random.random您将得到一个介于 0 和 1 之间的数字。

回答by Martijn Pieters

random.random()gives you a random floating point number in the range [0.0, 1.0)(so including 0.0, but not including 1.0which is also known as a semi-open range). random.uniform(a, b)gives you a random floating point number in the range [a, b], (where rounding may end up giving you b).

random.random()为您提供范围内的随机浮点数[0.0, 1.0)(因此包括0.0,但不包括1.0也称为半开放范围)。random.uniform(a, b)给你一个范围内的随机浮点数[a, b],(四舍五入可能最终给你b)。

The implementation of random.uniform()uses random.random()directly:

直接使用的实现random.uniform()random.random()

def uniform(self, a, b):
    "Get a random number in the range [a, b) or [a, b] depending on rounding."
    return a + (b-a) * self.random()

random.uniform(0, 1)is basically the same thing as random.random()(as 1.0times float value closest to 1.0still will give you float value closest to 1.0there is no possibility of a rounding error there).

random.uniform(0, 1)基本上是同样的事情random.random()(如1.0浮点值最接近1.0仍然会给你浮点值最接近1.0没有一个舍入误差的存在的可能性)。

回答by Saurabh Adhikary

In random.random() the output lies between 0 & 1 , and it takes no input parameters

random.random() 中,输出介于 0 和 1 之间,并且不需要输入参数

Whereas random.uniform()takes parameters , wherein you can submit the range of the random number. e.g.
import random as ra print ra.random() print ra.uniform(5,10)

random.uniform()接受 parameters ,您可以在其中提交随机数的范围。例如
import random as ra print ra.random() print ra.uniform(5,10)

OUTPUT:-
0.672485369423 7.9237539416

输出:-
0.672485369423 7.9237539416

回答by Daksh

Apart from what is being mentioned above, .uniform()can also be used for generating multiple random numbers that too with the desired shape which is not possible with .random()

除了上面提到的之外,.uniform()还可以用于生成多个随机数,这些随机数也具有所需的形状,这是不可能的.random()

np.random.seed(99)
np.random.random()

#generates 0.6722785586307918

while the following code

而下面的代码

np.random.seed(99)
np.random.uniform(0.0, 1.0, size = (5,2))

#generates this 
array([[0.67227856, 0.4880784 ],
       [0.82549517, 0.03144639],
       [0.80804996, 0.56561742],
       [0.2976225 , 0.04669572],
       [0.9906274 , 0.00682573]])

This can't be done with random(...), and if you're generating the random(...) numbers for ML related things, most of the time, you'll end up using .uniform(...)

这不能用 random(...) 来完成,如果你为 ML 相关的东西生成 random(...) 数字,大多数时候,你最终会使用 .uniform(...)