augustspies Posted June 16, 2011 Share Posted June 16, 2011 (edited) Hi guys, Does anyone know how to setup a script to run @ random times? I currently have my script as a scheduled task to run every 30mins, however looking to randomize the run times. this is what i have so far however, I have the below on a scheduled task to run every hr and the loop to run 4x/ hr. the issue i'm running into is that the 4th time it runs it could still be running into the next hr. (hopefully that makes sense) $mintimedelay = (5000); (15)*60*1000 (want to wait min 15 mins between each script run) $staggerlikedat = (2000); (4)*60*1000 $count = 0 $maxcount = 4 While 1 $runme = "" Run($runme, @ScriptDir) $howlong = $mintimedelay + random(0, $staggerlikedat, 1) ConsoleWrite(@CRLF & "Next run in: " & ($howlong/1000) & " sec...." & @CRLF); $count+=1 if $count = $maxcount then ExitLoop Sleep($howlong) WEnd Edited June 16, 2011 by augustspies Link to comment Share on other sites More sharing options...
sleepydvdr Posted June 16, 2011 Share Posted June 16, 2011 (edited) Depending on how complicated you want to make your code, you have lots of options. Something like this might work: While 1 $runme = "" Run($runme, @ScriptDir) $howlong = $mintimedelay + random(0, $staggerlikedat, 1) If $howlong >= $mintimedelay Then ; If the last quarter is greater or equal to 60 minutes, make the remaining time be the difference between 60 minutes and the last random number $howlong = $mintimedelay - $howlong EndIf ConsoleWrite(@CRLF & "Next run in: " & ($howlong/1000) & " sec...." & @CRLF); $count+=1 if $count = $maxcount then ExitLoop Sleep($howlong) WEnd I think a more efficient method would be to pick a random time between 0-15 minutes (average of 7.5 minutes). Then pick a random number between the last value and 15 minutes. Then a random number between that value and 30 minutes. And again, to 45 minutes. The laziest way to do it would be create three variables ($firstQuarter, $secondQuarter, $thirdQuarter) and use them to pit against the random function times. This way your random times couldn't all be at the very beginning or the very end. They would have to be staggered throughout the hour (but still random). Edited June 17, 2011 by sleepydvdr #include <ByteMe.au3> Link to comment Share on other sites More sharing options...
sleepydvdr Posted June 16, 2011 Share Posted June 16, 2011 (edited) Better example: Global $firstQuarter, $secondQuarter, $thirdQuarter $runme = "whatever.exe" _RandomTimes() Func _RandomTimes() $firstQuarter = Random(1, 15, 1) $secondQuarter = Random($firstQuarter, 30, 1) $thirdQuarter = Random($secondQuarter, 45, 1) Sleep($firstQuarter * 60000) ; 60000 is one minute. This statement results in a random time between 1 and 15 minutes. Run($runme) Sleep($secondQuarter * 60000) Run($runme) Sleep($thirdQuarter * 60000) Run($runme) Sleep(600000 - $thirdQuarter * 60000) ; 60 minutes minus the leftover time Run($runme) EndFunc Edited June 17, 2011 by sleepydvdr #include <ByteMe.au3> Link to comment Share on other sites More sharing options...
sleepydvdr Posted June 17, 2011 Share Posted June 17, 2011 (edited) And if you want to run at completely random times during that hour: #include <array.au3> Global $firstRun, $secondRun, $thirdRun, $fourthRun $runme = "whatever.exe" _RandomTimes() Func _RandomTimes() $firstRun = Random(1, 60, 1) $secondRun = Random(1, 60, 1) $thirdRun = Random(1, 60, 1) $fourthRun = Random(1, 60, 1) Dim $array[4] = [$firstRun, $secondRun, $thirdRun, $fourthRun] _ArraySort($array) Sleep($array[0] * 60000) Run($runme) Sleep($array[1] * 60000) Run($runme) Sleep($array[2] * 60000) Run($runme) Sleep($array[3] * 60000) Run($runme) EndFunc Edited June 17, 2011 by sleepydvdr #include <ByteMe.au3> Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now