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

5

78

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?

Collings answered 4/5, 2015 at 12:51 Comment(3)
Have you read the documentation? random.uniform(0, 1) is the same as random.random().Morice
There is a difference, due to floating-point rounding you can sometimes hit the upper bound in uniformFranza
Actually according to the documentation they are not the same...Filament
S
103

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).

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

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).

Sandasandakan answered 4/5, 2015 at 12:58 Comment(0)
S
11

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

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)

OUTPUT:-
0.672485369423 7.9237539416

Shirashirah answered 15/11, 2016 at 9:34 Comment(0)
K
11

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()

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(...)

Karnak answered 17/12, 2018 at 14:43 Comment(2)
This is for numpy's random module, which is different than the Python stdlib's random module. Still, very useful to know!Totaquine
What is np in this code? Also, I tried using random.uniform(0.0, 1.0, size = (5,2)) and was given an error that size was an unexpected keyword argument.Fireball
R
6

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.

Rupture answered 4/5, 2015 at 12:57 Comment(0)
L
2

According to the documentation on 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).

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.

Lippert answered 4/5, 2015 at 12:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.