random模块的uniform(x, y)方法,会生成一个介于x,y之间的实数,先来看下该函数的帮助文档
>>> help(random.uniform)
Help on method uniform in module random:
uniform(a, b) method of random.Random instance
Get a random number in the range [a, b) or [a, b] depending on rounding.
用法很简单,uniform(x, y),就会生成一个介于x,y之间的一位尾数不定的实数
>>> random.uniform(1,8)
1.1222117272038796
>>> random.uniform(4,8)
7.872998276235861
实际项目中,要造6位经纬度(-180, 180)的随机数据,就可以用random.uniform很完美的来完成任务
>>> def func(x=-180, y=180):
... import random
... return "%.6f" % random.uniform(x, y)
>>> func()
'97.615427'