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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With