1

I am trying to use the code:

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.

AEA
  • 213
  • 2
  • 12
  • 34

2 Answers2

4

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

random() -> x in the interval [0, 1).

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).

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

abdulhaq-e
  • 626
  • 9
  • 19
1

random.random is not the function you want. You want to use random.randrange or random.randint depending on your version.

See http://docs.python.org/2/library/random.html for more details.

Richard Schneider
  • 34,944
  • 9
  • 57
  • 73
  • random.random is a valid function: http://docs.python.org/2/library/random.html#random.random It returns a random float between 0 and 1.0 – karthikr Jun 05 '13 at 01:46