【问题标题】:JavaScript: Getting random location on globeJavaScript:获取地球上的随机位置
【发布时间】:2017-09-14 04:14:16
【问题描述】:

我正在尝试创建一个返回地球上随机点的函数。当我尝试下面的代码时,它最终会在两极聚集更多的点,而在赤道聚集更少的点。

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
}

我应该怎么做?

注意:这个点可能在一个地球大小的球体上,但也可以是任何其他大小。我只需要创建随机均匀分布的经纬度。

【问题讨论】:

  • 随机均匀分布”不是随机的。另见:stackoverflow.com/questions/5715826/…
  • @Teemu Random 意味着计算机为我选择一个随机数并且均匀分布意味着它不会聚集在球体的顶部和底部。

标签: javascript random geolocation


【解决方案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
  }
}

【讨论】:

    猜你喜欢
    • 2021-10-06
    • 1970-01-01
    • 1970-01-01
    • 2016-01-18
    • 2018-02-15
    • 2011-06-15
    • 2015-04-06
    • 1970-01-01
    • 2023-04-07
    相关资源
    最近更新 更多