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?
-
4Have you read [the documentation](https://docs.python.org/2/library/random.html)? `random.uniform(0, 1)` is the same as `random.random()`. – jonrsharpe May 04 '15 at 12:53
-
1There is a difference, due to floating-point rounding you can sometimes hit the upper bound in uniform – Padraic Cunningham May 04 '15 at 12:56
-
6Actually according to the documentation they are not the same... – Roman Kutlak May 04 '15 at 12:57
5 Answers
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).

- 12,289
- 5
- 46
- 47

- 1,048,767
- 296
- 4,058
- 3,343
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

- 355
- 5
- 14
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(...)

- 7,554
- 2
- 25
- 36
-
This is for numpy's random module, which is different than the Python stdlib's random module. Still, very useful to know! – Michael Scott Asato Cuthbert Jun 12 '22 at 00:19
-
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. – Sabatino Ognibene Feb 16 '23 at 20:52
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.

- 398,270
- 210
- 566
- 880
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.

- 4,070
- 3
- 17
- 26