I am trying to work out how to return a random time between 2 time ranges. For example, get a random time between 15:00:00 and 22:00:00
So far, I have - but don't know how to limit it
select sec_to_time(floor(15 + (rand() * 86401)))
Any help would be much appreciated!
SELECT SEC_TO_TIME(
FLOOR(
TIME_TO_SEC('15:00:00') + RAND() * (
TIME_TO_SEC(TIMEDIFF('22:00:00', '15:00:00'))
)
)
);
Calculate in seconds. Then add a random number that is in the range of seconds between 15:00:00
and 22:00:00
to 15:00:00
. Then convert the seconds back to a time value.
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