我想在给定的时间范围内生成随机连续的日期。我已经看到了一些生成随机日期的方法,下面我在一个函数中进行了一次尝试,但我认为有一种更紧凑的方法可以做到这一点。任何帮助都将不胜感激!
该函数做了几件事:
- 从日期窗口中的随机起点创建连续日期
- 这些连续日期的可变长度在2-5天之间
- 该函数确保在给定的开始日期和结束日期之间选择连续的日期
代码如下。有没有更紧凑的方法来生成这个函数?
def illness(start_date,end_date):
funct_ill_list=[] #list that will store all dates
#randomly choosing first date
diff = end_date - start_date
random_number = random.randint(0,diff.days-5)
temp = start_date + datetime.timedelta(random_number) #temp = 1 march + "random_number" days
funct_ill_list.append(temp) #adding the first date to list
#adding next 'n' (2-5) consecutive dates after our last element in list(most recently added date in list)
# ----------------FOR EXAMPLE - funct_ill_list = ['4 march','5 march','6 march']; random_number=3
while funct_ill_list[-1]<=end_date: #stop when last element in list (most recently added date in list) exceeds end_date
random_number = random.randint(2,5) #for 2-5 random days (consecutive) - get a random integer between 2 to 5
last = funct_ill_list[-1]+datetime.timedelta(random_number) #'last' variable stores maximum possible date we will have with chosen random_number
if last>end_date:
funct_ill_list.pop()
break
else: #else add next 'random_number' dates to list
ref_date = funct_ill_list[-1] #last element of list
for i in range(1,random_number): #'i' takes values from 1 to (random_number-1).
temp = ref_date + datetime.timedelta(i) #each time add 'i' days to ref_date to get new date 'temp'
funct_ill_list.append(temp) #add this new date to list.
#for next random date
# --------------FOR EXAMPLE - funct_ill_list = ['4 march','5 march','6 march','25 march']
diff = end_date - funct_ill_list[-1] #get no. of days remaining b/w end_date and last element added to list
if diff.days>=2: #if diff.days=0 or 1, ie. last element in list is either end_date or end_date-1,
#No point of adding more dates for next round (since 2-5 consecutive days won't be possible), else add.
random_number = random.randint(2,diff.days) #randomly choose a number
temp = funct_ill_list[-1] + datetime.timedelta(random_number) #adding "random_number" days to last element of list
funct_ill_list.append(temp) #adding it to list
return funct_ill_list
ill_start_date = datetime.date(2020, 2, 1)
ill_end_date = datetime.date(2020, 4, 30)
month_time_frame = [3,5] #static two months time frame (Looks at matching dates in March and May months)
如果日期不满足条件,您实际上不需要太多迭代和删除日期,因为这里没有太多随机性。你只需要两个随机数:
- 您的退货价值将有多少天?让我们称之为
m
,这是一个在[2, 5]
范围内的整数 - 在
start_date
-end_date
的时间间隔中,我们应该在什么时候开始我们的日期序列?或者,我们的第一次约会应该在start_date
之后几天?让我们称之为N
很容易注意到,N
受到给定开始-结束范围内的天数(我们称之为R
(和疾病持续时间(m
(的限制。具体来说,我们知道N <= R - m
。
一旦我们有了N
,我们就知道我们想要的日期是:
start_date
+N
天start_date
+N + 1
天- 。。。等等
start_date
+N + m - 1
天
这可以使用start_date
+range(N+m)
天的简单循环或列表理解生成
import random
import datetime
def illness(start_date, end_date):
# Duration of start-end range (R)
max_days = (end_date - start_date).days
# How many days should the illness start (m)
illness_days = random.randint(2, min(max_days, 5))
# Number of days after start_date that the first random date is on (N)
illness_start = random.randint(0, max_days - illness_days)
# Make a list of illness_days consecutive dates starting from the first illness day
illness_dates = [start_date + datetime.timedelta(days=illness_start + d) for d in range(illness_days)]
return illness_dates
运行此:
sd = datetime.date(2021, 1, 1)
ed = datetime.date.today()
illness(sd, ed)
给出随机(在本例中为4(个连续日期,这些日期随机位于给定间隔内:
[datetime.date(2021, 4, 26),
datetime.date(2021, 4, 27),
datetime.date(2021, 4, 28),
datetime.date(2021, 4, 29)]