Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (47.6k points)

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?

1 Answer

0 votes
by (106k points)

The difference between random.random() and random.uniform()is as follows:-

The random.uniform()  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.

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

def uniform(self, a, b): 

return a + (b-a) * self.random()

To know more about this you can have a look at the following video tutorial:-

Related questions

0 votes
1 answer
asked Sep 10, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
0 votes
1 answer
asked Sep 17, 2019 in Python by Sammy (47.6k points)
0 votes
4 answers
asked Mar 31, 2021 in Python by laddulakshana (16.4k points)

Browse Categories

...