Python – Generate k random dates between two other dates
Given two dates, the task is to write a Python program to get K dates randomly.
Input : test_date1, test_date2 = date(2015, 6, 3), date(2015, 7, 1), K = 7
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
Output : [datetime.date(2015, 6, 18), datetime.date(2015, 6, 25), datetime.date(2015, 6, 29), datetime.date(2015, 6, 11), datetime.date(2015, 6, 11), datetime.date(2015, 6, 10), datetime.date(2015, 6, 24)]
Explanation : 7 random dates starting from 3 June 2015 to 1 July 2015 are generated.
Input : test_date1, test_date2 = date(2015, 6, 3), date(2015, 8, 1), K = 6
Output : [datetime.date(2015, 6, 20), datetime.date(2015, 7, 22), datetime.date(2015, 6, 29), datetime.date(2015, 6, 18), datetime.date(2015, 6, 13), datetime.date(2015, 7, 14)]
Explanation : 6 random dates starting from 3 June 2015 to 1 August 2015 are generated.
Method #1 : Using choices() + loop + timedelta()
In this, we extract all dates in range using loop and timedelta() of 1-day difference. Among them K random dates are chosen using choices().
Python3
# Python3 code to demonstrate working of# Random K dates in Range# Using choices() + timedelta() + loopfrom datetime import date, timedeltafrom random import choices # initializing dates ranges test_date1, test_date2 = date(2015, 6, 3), date(2015, 7, 1) # printing dates print("The original range : " + str(test_date1) + " " + str(test_date2)) # initializing KK = 7 res_dates = [test_date1] # loop to get each date till end datewhile test_date1 != test_date2: test_date1 += timedelta(days=1) res_dates.append(test_date1) # random K dates from packres = choices(res_dates, k=K) # printing print("K random dates in range : " + str(res)) |
Output:
The original range : 2015-06-03 2015-07-01
K random dates in range : [datetime.date(2015, 6, 25), datetime.date(2015, 6, 10), datetime.date(2015, 6, 18), datetime.date(2015, 6, 7), datetime.date(2015, 6, 4), datetime.date(2015, 6, 16), datetime.date(2015, 6, 12)]
Method #2 : Using randrange() + timedelta() + loop
In this, a total number of days between ranges is extracted and that range is used to get elements K times, adding a random index number from start date extracted using randrange().
Python3
# Python3 code to demonstrate working of# Random K dates in Range# Using randrange() + timedelta() + loopfrom datetime import date, timedeltaimport random # initializing dates ranges test_date1, test_date2 = date(2015, 6, 3), date(2015, 7, 1) # printing dates print("The original range : " + str(test_date1) + " " + str(test_date2)) # initializing KK = 7 # getting days between datesdates_bet = test_date2 - test_date1total_days = dates_bet.days res = []for idx in range(K): random.seed(a=None) # getting random days randay = random.randrange(total_days) # getting random dates res.append(test_date1 + timedelta(days=randay)) # printing print("K random dates in range : " + str(res)) |
Output:
The original range : 2015-06-03 2015-07-01
K random dates in range : [datetime.date(2015, 6, 26), datetime.date(2015, 6, 5), datetime.date(2015, 6, 6), datetime.date(2015, 6, 18), datetime.date(2015, 6, 21), datetime.date(2015, 6, 15), datetime.date(2015, 6, 12)]


