How to choose a random time once per hour -
suppose want run task 1 time per hour, @ variable time during hour. doesn't have random; don't want @ top of hr every hour, example. , want 1 time per hr only.
this eliminates several obvious approaches, such sleeping random amount of time between 30 , 90 minutes, sleeping again. possible (and pretty likely) task run several times in row sleep of little more 30 minutes.
the approach i'm thinking looks this: every hour, hash unix timestamp of hour, , mod result 3600. add together result unix timestamp of hour, , that's moment when task should run. in pseudocode:
while = clock.tick; // = unix timestamp hr = - % 3600; hash = md5sum(hour); the_time = hr + hash % 3600; if == the_time; do_the_work(); end end
i'm sure meet requirements, thought fun throw question out , see ideas other people have!
for next hr work in, pick random min within hour.
that is, pick random time next interval work in; might same interval (hour) current interval (hour) if work has carried on previous interval.
the "time sleep" time until then. execute "immediately" on carry-over situation if random time before now: ensure random time picked each hour, unless work takes more hour.
don't create more complex has - there no reason hash or otherwise muck random
here. how "enterprise" solutions sharepoint timers (with hourly schedule) work.
random time
No comments:
Post a Comment