Skip to main content
Became Hot Network Question
edited tags
Link
user2856
  • 69.6k
  • 6
  • 119
  • 203
tyop; title as statement
Link
Vince
  • 20.3k
  • 16
  • 48
  • 65

How to generate Generate random lotlon/lat coordinate anywhere on the map?

Source Link
pileup
  • 151
  • 4

How to generate random lot/lat coordinate anywhere on the map?

I want to generate a random coordinate that would display anywhere on the map. I've seen these two posts:

Generating random lat/long coordinates

And using this post for the JavaScript code: https://stackoverflow.com/questions/69221354/how-to-generate-random-longitude-and-latitude

And came up with the following:

function generateRandomLonLat() {
  return [Math.floor(Math.random() * 360) - 180, Math.round(Math.acos(2 * Math.random() - 1) * 180 / Math.PI) - 90];
}

But it also generates non-valid coordinates that are outside of the map.

Why?