I am trying to generate synthetic data with with date & time range. How can I include the needed date-time section in the program mentioned below?
import random
import sys
import math
latitude = 0.794501
longitude = -0.752568
file_n = 'random_lat_lon.csv'
def generate_random_data(lat, lon, num_rows, file_name):
with open(file_name, 'w') as output:
for _ in xrange(num_rows):
hex1 = '%012x' % random.randrange(16**12)
flt = float(random.randint(0,100))
dec_lat = random.random()/100
dec_lon = random.random()/100
output.write('%s,%.1f,%.6f,%.6f \n' % (hex1.lower(), flt, lon+dec_lon, lat+dec_lat))
generate_random_data(latitude, longitude, 600, file_n)
Include two columns of Start_DateTime & End_DateTime
along other columns in the output.
Appreciate the help, Thank in advance.
You can generate random dates between a date range using datetime.timedelta, create a function that generates random start and end dates:
import datetime
import random
def get_random_dates():
start = datetime.date(2007, 1, 1)
end = datetime.date(2017, 1, 1)
d1 = start + datetime.timedelta(
seconds=random.randint(0, (end - start).total_seconds()))
d2 = start + datetime.timedelta(
seconds=random.randint(0, (end - start).total_seconds()))
# make sure start_date is before end_date
return sorted([d1, d2])
Then you can get random start and end times by calling the function:
startdatetime, enddatetime = get_random_dates()
And write these to your csv file along with other random generated data:
output.write('%s,%.1f,%.6f,%.6f,%s,%s \n' % (hex1.lower(),
flt,
lon+dec_lon,
lat+dec_lat,
str(startdatetime),
str(enddatetime)))