我正在尝试创建一个返回地球上随机点的函数。当我尝试下面的代码时,它最终聚集在极点处的更多点,而在赤道处则更少。
function random_location() {
return {
lon: Math.floor(Math.random() * 360) - 179,
lat: Math.floor(Math.random() * 181) - 90
}
}
for (var i = 0; i < 100; i++) {
var location = random_location();
// do something
}
我该怎么做?
注意:此点可能位于地球大小的球体上,但也可以是任何其他大小。我只需要创建随机均匀分布的纬度和经度。
答案 0 :(得分:1)
来自http://mathworld.wolfram.com/SpherePointPicking.html:
function random_location() {
return {
lon: Math.floor(Math.random()*360) - 180,
lat: Math.round(Math.acos(2*Math.random() - 1)*180/Math.PI) - 90
}
}