I am trying to create random longitude and latitudes. I need to create numbers between
-180.000 and +180.000. So, I might get 101.325 or -3.546 or -179.561.
Can you tell me a quick formula for that?
Thanks to everyone for your help. I have combined a couple of examples to suit my needs. Yes, I could shorten the code, but this really helps to see what’s going on.
// LONGITUDE -180 to + 180
function generateRandomLong() {
var num = (Math.random()*180).toFixed(3);
var posorneg = Math.floor(Math.random());
if (posorneg == 0) {
num = num * -1;
}
return num;
}
// LATITUDE -90 to +90
function generateRandomLat() {
var num = (Math.random()*90).toFixed(3);
var posorneg = Math.floor(Math.random());
if (posorneg == 0) {
num = num * -1;
}
return num;
}
In your case:
getRandomInRange(-180, 180, 3):