How to get a random number between a float range?

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


使用random.uniform(a,b):

>>> random.uniform(1.5, 1.9)
1.8733202628557872

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.


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

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

the second argument is the number of decimals.

链接地址: http://www.djcxy.com/p/96732.html

上一篇: OCR应用程序之前的图像清理

下一篇: 如何获得浮动范围之间的随机数字?