【问题标题】:Use of the random function to randomise scheduled tasks使用 random 函数随机化计划任务
【发布时间】:2013-05-31 13:56:11
【问题描述】:

我正在尝试使用代码:

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)

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

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

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

【问题讨论】:

    标签: python python-2.7 random scheduled-tasks scheduling


    【解决方案1】:

    random.random 不是你想要的函数。您想使用random.randrangerandom.randint,具体取决于您的版本。

    更多详情请见http://docs.python.org/2/library/random.html

    【讨论】:

    【解决方案2】:

    在终端中输入help(random.random)(或random.random??,如果你使用的是iPython),你会得到:

    random() -&gt; x in the interval [0, 1).

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

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

    【讨论】:

      猜你喜欢
      • 2020-06-14
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-02
      相关资源
      最近更新 更多