使用随机函数随机化定时任务 [英] Use of the random function to randomise scheduled tasks

查看:102
本文介绍了使用随机函数随机化定时任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用代码:

import random
import datetime
from sched import scheduler
from time import time, sleep

s = scheduler(time, sleep)
random.seed()

def run_periodically(start, end, interval, func):
    event_time = start
    while event_time < end:
        s.enterabs(event_time, 0, func, ())
        event_time += interval + random.random(-5, 45)
    s.run()

getData()#######

run_periodically(time()+5, time()+1000000, 10, getData)

我正在尝试获取预定时间和/或预定间隔以进行一定程度的随机化,目前代码返回

I am trying to get the scheduled time and or the scheduled interval to have some degree of randomisation, currently the code returns the

typeError: random() takes no arguments (2 given)

如果有人能告诉我如何解决这个问题或提供替代方法,我们将不胜感激.

If anyone could either tell me how to fix this or provide an alternative method it would be greatly appreciated.

推荐答案

在终端输入 help(random.random)(或者 random.random?? 如果你'正在使用 iPython),你会得到:

Type help(random.random) in the terminal (or random.random?? if you're using iPython) and you'll get:

random() ->x 在区间 [0, 1).

因此它不需要任何输入,这是错误背后的原因.要生成某个范围内的随机数,您可以使用 random.randint(链接到 random.randrange).

So it doesn't take any input which is the reason behind the error. To generate a random number within a certain range you can use random.randint (which is linked to random.randrange).

所以你的情况是这样的:random.randint(-5,45)

So it would be like this for your case: random.randint(-5,45)

这篇关于使用随机函数随机化定时任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆