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

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 on
lowerbound and upperbound numbers which must be passed as arguments.

Syntax: random LowerBound UpperBound

Example: random 1 10

Non-numeric arguments will be evaluated as 0.
An argument of /? or -? displays this syntax and always returns 0.


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

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.