这是我为做到这一点而准备的一个功能。它以小数点数作为参数,并使用对偶数/奇数随机数的检查来使正/负随机化。
$latlon = randomLatLon(4);
print_r($latlon);
function randomLatLon($granularity) {
// $granularity = Number of decimal spaces.
$power = pow(10,$granularity); // Extended 10 to the power of $granularity.
// Generate the lat & lon (as absolutes) according to desired granularity.
$lat = rand(0,90 * $power) / $power;
$lon = rand(0,180 * $power) / $power;
// Check if a random number is even to randomly make the lat/lon negative.
if (rand(0,100) % 2 == 0) {
$lat = $lat * -1;
}
// Same for lon...
if (rand(0,100) % 2 == 0) {
$lon = $lon * -1;
}
return array(
"lat" => $lat,
"lon" => $lon,
);
}