JSI Tip 7375. How do I generate a random number between a lower and upper bound?

Jerold Schulman

October 26, 2003

1 Min Read
ITPro Today logo in a gray background | ITPro Today

Marty List brings us Random.zip to return %ERRORLEVEL% as a random number between a lowerbound and upperbound parameter.

When you type random /?, you receive:

Returns an exit code (Errorlevel) of a random number based onlowerbound and upperbound numbers which must be passed as arguments.Syntax: random LowerBound UpperBoundExample: random 1 10Non-numeric arguments will be evaluated as 0.An argument of /? or -? displays this syntax and always returns 0.

Example:

To set an environment variable to a random number between 1 and 997:

for /f "Tokens=1" %%r in ('random 1 997') do if /i "%%r" NEQ "The" set /a rnum=%%r



Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like