How to get a random number between a float range?


Question

randrange(start, stop) only takes integer arguments. So how would I get a random number between two float values?

1
348
1/17/2017 1:56:37 PM

Accepted Answer

Use random.uniform(a, b):

>>> random.uniform(1.5, 1.9)
1.8733202628557872
554
5/22/2011 1:04:33 PM

random.uniform(a, b) appears to be what your looking for. From the docs:

Return a random floating point number N such that a <= N <= b for a <= b and b <= N <= a for b < a.

See here.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon