简体   繁体   English

random.random()与random.uniform()是否相同?

[英]Is random.random() the same as random.uniform()?

What is the difference between python's random.random() method and random.uniform() method? python的random.random()方法和random.uniform()方法有什么random.uniform()

Are the result from random.random() not uniformly distributed? 是否来自random.random()的结果分布不均匀?

import random

print(random.random() * 10)
print(random.uniform(0,10))

This is the definition of random.uniform from the random.py module: 这是random.py模块中random.uniform的定义:

def uniform(self, a, b):
    return a + (b-a) * self.random()

So, yes, it's the same distribution. 所以,是的,它是相同的分布。

According to documentation , random.random() returns a number in the range [0, 1.0), when random.uniform(a,b) returns a number in range [a,b] or [a,b), depending on floating-point rounding in equation a + (ba) * random() . 根据文档random.random()返回范围为[ random.uniform(a,b)的数字,而random.uniform(a,b)返回范围为[a,b]或[a,b)的数字,具体取决于浮点数方程a + (ba) * random()小数点舍入。

So both print(random.random() * 10) and print(random.uniform(0,10)) can give you the same distribution. 因此, print(random.random() * 10)print(random.uniform(0,10))都可以提供相同的分布。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM