Friday, November 5, 2010

Random times in a batch file

You can use random times in a batch file using something like that:

echo randomize >C:\TMP\random.vbs
echo wscript.sleep Int(Rnd * 100000) >>C:\TMP\random.vbs
cscript C:\TMP\random.vbs
dir

Here is a more elaborated example (only for the vbs script):

TopWait=8       'Max. time to wait in hours
randomize
myrand = rnd()
Wait=Int(myrand * 3600 * TopWait)  'Time to wait in seconds

Hours=Wait \ 3600
Minutes=(Wait - (Hours*3600)) \ 60
Seconds=Wait - (Hours*3600) - (Minutes*60)

WScript.Echo "Waiting: " & Wait & " seconds => " & Hours & ":" & Minutes & ":" & Seconds
wscript.sleep Wait * 1000    'Must be in miliseconds
WScript.Echo "Done: " & Wait & " seconds sleeping"

REFERENCES

WScript.Sleep
Suspend the execution of the current script for the specified number of milliseconds.

Syntax
WScript.Sleep lngTime

Arguments:
lngTime is the delay in milliseconds

Example
WScript.Sleep(5000)
WScript.Echo("5 seconds have passed.")

Rnd
Return a random number

Syntax
Rnd[(seed)]

Key
seed A seed value of 0 will return the most recent random number
A seed value > 0 will return the next random number(default)
A seed value < 0 will return the same number

Example
myrand = rnd()
WScript.Echo myrand

No comments:

Post a Comment