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?
在随机模块的python中,random.uniform()和random.random()有什么区别?它们都生成伪随机数,random.uniform()从均匀分布生成数字,random.random()生成下一个随机数。有什么不同?
5 个解决方案
#1
random.random()
gives you a random floating point number in the range [0.0, 1.0)
(so including 0.0
, but not including 1.0
which 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.0
times float value closest to 1.0
still will give you float value closest to 1.0
there is no possibility of a rounding error there).
random.uniform(0,1)与random.random()基本相同(1.0最接近1.0的浮点值仍然会给你最接近1.0的浮点值,那里不存在舍入误差)。
#2
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()接受参数,其中您可以提交随机数的范围。例如随机导入为ra print ra.random()print ra.uniform(5,10)
OUTPUT:-0.672485369423 7.9237539416
输出: - 0.672485369423 7.9237539416
#3
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)指定不同的范围。
#4
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.
while random.random
:
Return the next random floating point number in the range [0.0, 1.0).
返回[0.0,1.0]范围内的下一个随机浮点数。
I.e. with random.uniform
you specify a range you draw pseudo-random numbers from, e.g. between 3 and 10. With random.random
you get a number between 0 and 1.
即使用random.uniform,您可以指定从中绘制伪随机数的范围,例如使用random.random,你得到一个介于0和1之间的数字。
#5
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(...)
这不能用随机(...)来完成,如果你为ML相关的东西生成随机(...)数字,大多数时候,你最终会使用.uniform(...) )
#1
random.random()
gives you a random floating point number in the range [0.0, 1.0)
(so including 0.0
, but not including 1.0
which 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.0
times float value closest to 1.0
still will give you float value closest to 1.0
there is no possibility of a rounding error there).
random.uniform(0,1)与random.random()基本相同(1.0最接近1.0的浮点值仍然会给你最接近1.0的浮点值,那里不存在舍入误差)。
#2
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()接受参数,其中您可以提交随机数的范围。例如随机导入为ra print ra.random()print ra.uniform(5,10)
OUTPUT:-0.672485369423 7.9237539416
输出: - 0.672485369423 7.9237539416
#3
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)指定不同的范围。
#4
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.
while random.random
:
Return the next random floating point number in the range [0.0, 1.0).
返回[0.0,1.0]范围内的下一个随机浮点数。
I.e. with random.uniform
you specify a range you draw pseudo-random numbers from, e.g. between 3 and 10. With random.random
you get a number between 0 and 1.
即使用random.uniform,您可以指定从中绘制伪随机数的范围,例如使用random.random,你得到一个介于0和1之间的数字。
#5
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(...)
这不能用随机(...)来完成,如果你为ML相关的东西生成随机(...)数字,大多数时候,你最终会使用.uniform(...) )