If I wanted to do a command at a random time between two times how would I do this?
For example, send the following request at a random time between 10 seconds and 60 minutes?
import requests
r = requests.get('https://api.github.com/events')
If I wanted to do a command at a random time between two times how would I do this?
For example, send the following request at a random time between 10 seconds and 60 minutes?
import requests
r = requests.get('https://api.github.com/events')
The easiest implementation would be to make your program sleep for a random amount of time between 10 and 3600 seconds.
You can use the time.sleep()
method to make your program pause and the random.randint(x, y)
method to generate a random number between x
and y
import time, random, requests
time.sleep(random.randint(10, 3600))
r = requests.get('https://api.github.com/events')
You can use the random
module
import random
import time
secondstowait = random.randint(10, 60)
minutestowait = random.randint(0, 59) * 60 # converts minutes to seconds
time.sleep(secondstowait + minutestowait)
the sleep()
function from time
module obviously stops your program for the requested amount of seconds
While there are many ways of doing this, you could try to use full featured libraries to avoid reinventing the wheel and that can fulfill more complex requirements. Since your example snippet is python, please take a look at Simpy.
It is an excellent full featured library things like your example to more complex full featured simulations.