Search code examples
pythonrandomtimetimedelta

Generate random list of timestamps in python


I'm trying to generate random list of 24hr timestamps. I can generate one sample of date and time between the set range using the code below. I'm hoping to generate multiple samples (e.g. 10 samples)

Also, the date component isn't a priority for me. If i could drop that and just generate random 24hr timestamps that would be good.

Most threads I've found only consider generate random dates. I can find anything that concerns time.

import random
import time
from datetime import datetime

def randomDate(start, end):
    frmt = '%d-%m-%Y %H:%M:%S'

    stime = time.mktime(time.strptime(start, frmt))
    etime = time.mktime(time.strptime(end, frmt))

    ptime = stime + random.random() * (etime - stime)
    dt = datetime.fromtimestamp(time.mktime(time.localtime(ptime)))
    return dt

random_datetime = randomDate("20-01-2018 13:30:00", "23-01-2018 04:50:34")

print(random_datetime)

Output:

2018-01-21 03:33:55

Solution

  • The whole point of the datetime library: datetime, timedelta, etc. objects act as much like numbers as possible, so you can just do arithmetic on them.

    So, to generate a uniform distribution over a day, just take a uniform distribution from 0.0 to 1.0, and multiply it by a day:1

    td = random.random() * datetime.timedelta(days=1)
    

    To generate a uniform random time on some particular day, just add that to midnight on that day:

    dt = datetime.datetime(2018, 5, 1) + random.random() * datetime.timedelta(days=1)
    

    To generate a random timestamp between two timestamps:

    dt = random.random() * (end - start) + start
    

    And if you want 10 of those:

    [random.random() * (end - start) + start for _ in range(10)]
    

    That's all there is to it. Stay away from all those other time formats from the time module; they're only needed if you need compatibility with stuff like C libraries and filesystem data. Just use datetime in the first place:

    def randomtimes(start, end, n):
        frmt = '%d-%m-%Y %H:%M:%S'
        stime = datetime.datetime.strptime(start, frmt)
        etime = datetime.datetime.strptime(end, frmt)
        td = etime - stime
        return [random.random() * td + stime for _ in range(n)]
    

    1. However, keep in mind that if you're dealing with local rather than UTC times, some days are actually 23 or 25 hours long, because of Daylight Saving Time. A timedelta doesn't understand that.