How to Generate a Random Number in Batch Script
Generating random numbers is a key feature for scripts that need unpredictability. You might need a random number to select a file from a list, create a unique temporary ID, or to add a random delay to a network task. While batch scripting doesn't have a formal RAND() function, it provides a special, built-in dynamic variable, %RANDOM%, that makes this task simple and easy.
This guide will teach you how to use the %RANDOM% variable to get a random number, and more importantly, how to use the modulo operator to control the output and generate a random number within a specific, custom range.
The Core Variable: %RANDOM%
The %RANDOM% variable is a special, dynamic variable provided by the cmd.exe interpreter. It is not a stored environment variable (it won't show up in the SET command's output).
Each time you access %RANDOM%, it expands to a different pseudo-random decimal integer between 0 and 32767 (inclusive).
Basic Example: Getting a Random Number
This script simply demonstrates how %RANDOM% produces a different value each time it is called.
@ECHO OFF
ECHO Generating three random numbers...
ECHO.
ECHO Random Number 1: %RANDOM%
ECHO Random Number 2: %RANDOM%
ECHO Random Number 3: %RANDOM%
Output (will be different every time)
Generating three random numbers...
Random Number 1: 14572
Random Number 2: 3109
Random Number 3: 29881
The Key to Control: Generating a Random Number in a Range
Getting a number between 0 and 32767 is rarely what you need. More often, you'll need a number within a specific range, like 1 to 100, or 0 to 5. This is achieved using the modulo operator (%%) inside a SET /A arithmetic expression.
The modulo operator gives you the remainder of a division. For example, 13 %% 5 is 3 (because 13 divided by 5 is 2 with a remainder of 3).
The Formula to Get a Number from 0 to N-1: SET /A "RandomNumber = %RANDOM% %% N" where N is the number of possible values you want.
The Formula to Get a Number from Min to Max: SET /A "RandomNumber = (%RANDOM% %% (Max - Min + 1)) + Min"
Example: Get a Random Number between 1 and 100
- Range: We need 100 possible values (100 - 1 + 1).
- Formula:
(%RANDOM% %% 100) + 1
Example:
@ECHO OFF
SETLOCAL
ECHO --- Generating a random number between 1 and 100 ---
SET /A "RandomValue = (%RANDOM% %% 100) + 1"
ECHO The random number is: %RandomValue%
ECHO.
ECHO --- Generating a random number between 50 and 60 ---
SET /A "Min=50"
SET /A "Max=60"
SET /A "RandomValue = (%RANDOM% %% (Max - Min + 1)) + Min"
ECHO The random number is: %RandomValue%
ENDLOCAL
Let's see how the modulo formula works by breaking down (%RANDOM% %% 100) + 1:
%RANDOM%: Generates a large random number (e.g.,29881).%% 100: The modulo operator. The remainder of29881 / 100is81. The result of the modulo operation%RANDOM% %% 100will always be a number between 0 and 99.+ 1: We add 1 to the result to shift the range from0-99to the desired1-100.
This simple formula is the standard and most reliable way to constrain the output of %RANDOM%.
Common Pitfalls and How to Solve Them
-
Using Single
%for Modulo: The modulo operator is%. However, inside a batch script, a single percent sign is interpreted as the start of a variable name. To use a literal percent sign in a batch file, you must escape it by doubling it (%%).SET /A "Result = %RANDOM% % 10"-> WRONGSET /A "Result = %RANDOM% %% 10"-> CORRECT
-
Modulo Bias (Advanced Topic): The
%RANDOM%variable is not perfectly random, and using the modulo operator can introduce a slight bias. For example, in a0-32767range, some numbers in your smaller range might appear slightly more often than others. For scripting tasks like picking a random server or adding a delay, this bias is completely insignificant and can be ignored.
Practical Example: A Script with a Random Delay
This script simulates a task that needs to pause for a random amount of time between 3 and 10 seconds. This is a common technique to make automated network requests look less robotic.
@ECHO OFF
SETLOCAL
SET "MinDelay=3"
SET "MaxDelay=10"
ECHO --- Automated Task Runner ---
ECHO.
FOR /L %%i IN (1,1,5) DO (
ECHO Starting task %%i...
REM --- Calculate the random delay ---
SET /A "Delay = (%RANDOM% %% (MaxDelay - MinDelay + 1)) + MinDelay"
ECHO -> Task will run for a bit, then pause for %Delay% seconds.
REM (Simulate some work)
PING -n 2 127.0.0.1 > NUL
TIMEOUT /T %Delay% /NOBREAK > NUL
ECHO -> Task %%i complete.
ECHO.
)s
ECHO --- All tasks finished ---
ENDLOCAL
Conclusion
The built-in %RANDOM% variable is the simple and effective tool for introducing unpredictability into your batch scripts.
Key takeaways for its successful use:
%RANDOM%generates a number between 0 and 32767.- To get a number in a specific range, you must use the modulo operator (
%%) inside aSET /Acommand. - The standard formula for a range from
MintoMaxis:SET /A "Var = (%RANDOM% %% (Max - Min + 1)) + Min" - Always remember to double the percent sign (
%%) for the modulo operator inside a batch file.