Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating random latitude and longitude in MySQL

I'm trying to generate some dummy records in MySQL and need to create random latitude and longitude float values within a given range.

For example I need to generate latitudes that are between 52.077090052913654 and 52.477040512464626

and longitudes between -1.8840792500000134 and -0.9172823750000134

I'm familiar with creating ranges of random numbers using Rand Floor but that will only produce whole numbers. How I might do this?

like image 211
BarfordSimon Avatar asked Jul 10 '12 12:07

BarfordSimon


1 Answers

You can use RAND() to get a random value between two values like so:


randVal = (RAND() * (maxVal - minVal)) + minVal


With your numbers:

SELECT 
    (RAND() * (52.477040512464626 - 52.077090052913654)) + 52.077090052913654 AS randlatitude,
    (RAND() * (-0.9172823750000134 - -1.8840792500000134)) + -1.8840792500000134 AS randlongitude
like image 91
Zane Bien Avatar answered Oct 05 '22 23:10

Zane Bien