What is the difference between python's random.random()
method and random.uniform()
method?
Are the result from random.random()
not uniformly distributed?
import random
print(random.random() * 10)
print(random.uniform(0,10))
This is the definition of random.uniform
from the random.py
module:
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()
.
So both print(random.random() * 10)
and print(random.uniform(0,10))
can give you the same distribution.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.