title: How do I schedule a task at a random time via batch file?
body: <p>I need to create a script that will run via a .bat file on Windows 7 machines.
  The batch file will create a scheduled task that runs at a random time between 11am and 9pm on Sunday.
  I am currently using this command that I know works but only at a specific time:</p>
  
  <pre><code>schtasks /create /tn "task" /tr c:\task.vbs /sc WEEKLY /d SUN /st 21:00:00 /ru ""
  </code></pre>
  
  <p>I want to randomize the time, so that I can run the same exact batch file across all 400 systems and have each of them get a different time within that range, so they aren't running the task at the same time. I don't care if a few of them get identical times, but I don't want all 400 running within the same few minutes.
  Once I have a single batch file that works as intended, it would be pushed out to all systems and run one time to schedule that task.</p>
  
  <p>Based on searching other answers here, I've tried using the following scripts, for now just trying to generate a random hour, but I am not getting the results I need:</p>
  
  <p>Option 1)</p>
  
  <pre><code>set /a num=%random% * (21 - 11 + 1) / 32768 + 11
  </code></pre>
  
  <p>That command will generate a number within the correct range, but it's not really random, it seems to be based on the clock or something, because if I run it several times real quick, I get the same number and then it increments up as time passes, this wont work.</p>
  
  <p>Option 2)</p>
  
  <pre><code>@echo off &amp; setlocal EnableDelayedExpansion
  
  for /L %%a in (1 1 20) do (
          call:rand 11 21
          echo !RAND_NUM!
  ) &gt;&gt; random2.txt
  
  goto:EOF
  
  REM The script ends at the above goto:EOF.  The following are functions.
  
  REM rand()
  REM Input: %1 is min, %2 is max.
  REM Output: RAND_NUM is set to a random number from min through max.
  :rand
  SET /A RAND_NUM=%RANDOM% * (%2 - %1 + 1) / 32768 + %1
  goto:EOF
  </code></pre>
  
  <p>That code, generates the random number correctly, but I don't know how to insert that number into the command like I want. I have tried replacing the ">> random2.txt" with the schtasks command, but it doesn't work, and I'm not getting and feed back to a log file to see why, I've tried it like this:</p>
  
  <pre><code>@echo off &amp; setlocal EnableDelayedExpansion
  
  for /L %%a in (1 1 1) do (
          call:rand 11 21
          echo !RAND_NUM!
  )
  schtasks /create /tn "task" /tr c:\task.vbs /d SUN /sd 10/28/2012 /st %RANDOM%:00:00 /ru "" &gt; task.txt 2&gt;&amp;1
  goto:EOF
  
  REM The script ends at the above goto:EOF.  The following are functions.
  
  REM rand()
  REM Input: %1 is min, %2 is max.
  REM Output: RAND_NUM is set to a random number from min through max.
  :rand
  SET /A RAND_NUM=%RANDOM% * (%2 - %1 + 1) / 32768 + %1
  goto:EOF
  </code></pre>
  
  <p>So in that I've tried to use the %RANDOM% var as the hour component of the command.</p>
  
  <p>The perfect solution would generate a random two digit number between 11 and 21 for the hours section and then a two digit number between 00 and 59 for the minutes section of the command with a colon in between them as required by the format of the command
  so the time section of the schtasks command would look something like this:</p>
  
  <pre><code>%hour11-21%:%min00-59%:00
  </code></pre>
  
  <p>I hope I have given enough detail here, can anyone tell me what am I doing wrong? Is there an easier way to do this?
  Thank you in advance!</p>