How to get a random number between a float range?

2019-01-03 12:21发布

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

3条回答
在下西门庆
2楼-- · 2019-01-03 12:47

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.

查看更多
Lonely孤独者°
3楼-- · 2019-01-03 12:49

if you want generate a random float with N digits to the right of point, you can make this :

round(random.uniform(1,2), N)

the second argument is the number of decimals.

查看更多
迷人小祖宗
4楼-- · 2019-01-03 13:11

Use random.uniform(a, b):

>>> random.uniform(1.5, 1.9)
1.8733202628557872
查看更多
登录 后发表回答