如下所示,我有一个简单的for循环打印4-8次。
我试图让它在5小时内随机打印4-8次......
import random
for i in range(random.randrange(4,8+1)):
print i+1
print "do other stuff here..."
例如,它可能会执行以下操作::
22:30: 1 do other stuff here...
22:41: 2 do other stuff here...
22:45: 3 do other stuff here...
23:50: 4 do other stuff here...
00:33: 5 do other stuff here...
01:23: 6 do other stuff here...
02:20: 7 do other stuff here...
03:10: 8 do other stuff here...
因为它可能是随机的:
13:34: 1 do other stuff here...
13:41: 2 do other stuff here...
13:45: 3 do other stuff here...
13:50: 4 do other stuff here...
我怎样才能使这个工作,我无法弄清楚如何让两个循环一起运行..
Sleeping for: 274 minutes
do other stuff here...
Sleeping for: 10 minutes
do other stuff here...
Sleeping for: 13 minutes
do other stuff here...
Sleeping for: 1 minutes
do other stuff here...
Sleeping for: 0 minutes
do other stuff here...
Sleeping for: 0 minutes
do other stuff here...
Sleeping for: 0 minutes
do other stuff here...
Sleeping for: 0 minutes
do other stuff here...
total = 30
timer_list = sorted(random.randint(1, total) for i in range(random.randint(4, 8)))
timer_old = 0
timer_previous = 0
print timer_list
for counter, timer in enumerate(timer_list):
new_timer = timer_old - timer_previous
timer_previous = timer_old
sec = timedelta(seconds=int(new_timer))
d = datetime(1,1,1) + sec
if counter == 0:
print "First is instantatious"
else:
if d.hour and d.minute:
print "Sleeping for %d hour(s) %d minutes" % (d.hour, d.minute)
elif not d.hour and d.minute:
print "Sleeping for %d minutes and %d seconds" % (d.minute, d.second)
else:
print "Sleeping for %d seconds" % (d.second)
print "done"
time.sleep(timer - timer_old)
timer_old = timer
答案 0 :(得分:4)
获取4到8之间的随机数。使用这么多元素创建一个数组。为每个元素分配0到18000之间的随机值。对数组进行排序。循环遍历数组元素。在每个循环中,睡眠数组元素中的秒数减去您在之前循环中已经睡眠的秒数。打印“在这里做其他事情”。重复直到循环结束。
答案 1 :(得分:2)
这是另一种变体,基于Guntram的方法(以及我在问题下面的评论):
import random, time
total = 3600 * 5
times = sorted(random.randint(1, total) for i in range(random.randint(4, 8)))
last = 0
for t in times:
time.sleep(t - last)
last = t
print "Do something at %02d:%02d:%02d" % (t/3600, t/60%60, t%60)
这会在5小时间隔内创建4-8个时间点并对其进行排序。然后它在第一个间隔睡觉,做某事,为第一个和第二个的差异睡觉,再次做某事等等。
(可能的)优点是间隔彼此独立,并且应在整个5h间隔内均匀分布。在其他方法中,事件之间的间隔越来越小,因为它们是剩下的时间的随机量。
答案 2 :(得分:1)
import time
import random
from time import gmtime, strftime
max_time_to_sleep = 18000
min_time_to_sleep = 120
for i in range(random.randrange(4,8+1)):
sleep_for = random.randrange(max_time_to_sleep)
while sleep_for < min_time_to_sleep:
if max_time_to_sleep <= min_time_to_sleep:
break
sleep_for = random.randrange(max_time_to_sleep)
if sleep_for == 0:
sleep_for = min_time_to_sleep
time.sleep(sleep_for)
max_time_to_sleep -= sleep_for
if max_time_to_sleep < min_time_to_sleep:
max_time_to_sleep = min_time_to_sleep
cur_time = strftime("%H:%M:%S", gmtime())
print "%s : do other stuff here..." % cur_time
答案 3 :(得分:1)
import random, time
# time left within the 5 hour window
timeLeft = 5 * 3600
for i in range(random.randint(4, 8)):
# get a random sleep time from the remaining window and sleep for that time
sleepTime = random.randrange(timeLeft)
timeLeft -= sleepTime
time.sleep(sleepTime)
print('Execution #{}'.format(i))
# do stuff