JavaScript - How to create random longitude and latitudes?
function getRandomInRange(from, to, fixed) {
return (Math.random() * (to - from) + from).toFixed(fixed) * 1;
// .toFixed() returns string, so ' * 1' is a trick to convert to number
}
In your case: getRandomInRange(-180, 180, 3)
:
12.693
-164.602
-7.076
-37.286
52.347
-160.839
Math.random()*360 - 180
that will get you a range of -180 to 180
And if you really only want 3 decimal places
Math.round((Math.random()*360 - 180) * 1000)/1000