我正在创建一个模拟器,尝试模拟办公室门传感器。 办公室的营业时间通常为上午 8 点至下午 6 点。 该函数会在门打开和关闭时给我时间戳(在 Timestamps 中)。 门需要 10 秒才能关闭。
所以我想创建一个函数,我会在上午 8 点到下午 6 点之间随机调用该函数。该函数将为我提供门打开和关闭的时间戳。
模拟器每秒运行一个循环,我想在上午 8 点到下午 6 点之间随机调用此函数。
如果您能建议我一种不同的方法,我将不胜感激。
谢谢。
更新:假设门将保持关闭状态并显示Sensor_Status:0
,直到调用该函数。
所以我想将该函数命名为 def DoorOpenAction:
from apscheduler.schedulers.background import BackgroundScheduler
sched = BackgroundScheduler()
def DoorOpenAction():
print('I want to call this function anytime between 8.00am to 6.00pm')
print('I also want to do this even if a day is passed')
sched.add_job(DoorOpenAction, 'interval', seconds=1)
sched.start()
也许这很难, 我想知道我应该如何继续这件事。 我想创建一个模拟数据集
最佳答案
您可以在 DoorOpenAction() 内进行随机抽取,以确定您是否要执行该操作。您可以调整可能性以获得适当的频率。
关于python - 如何在给定的时间间隔内随机调用一个函数? - Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54784912/