Friday, April 03, 2009

How to generate random numbers in batch files

I bumped into this last week. There's an environment variable in Windows XP that returns a random number. You use it like this:

echo %RANDOM%

produces a 4-digit random number.

You can control the range of the number by specifying the order of magnitude...using a rather odd syntax:

echo %RANDOM:~-3%

produces values from 0 to 999;

echo %RANDOM:~-1%

produces values from 0 to 9.

No comments:

Post a Comment