I have a batch script that generates random numbers from 1 to 10000.

My Batch Script

@echo off
SET /A RAND=%RANDOM%%%10000+1
echo %RAND%

Current Outputs

2532  (4 digits)

253   (3 digits)

25    (2 digits)

2     (1 digits)

Expected Outputs

2432 (4 digits)

3431 (4 digits)

9282 (4 digits)

8812 (4 digits)


I need the random number to be consisted of 4 digits only and not 1, 2, or 3 digits and always 4.

  • 1
    See this comment. Aug 28, 2017 at 14:29
  • 2
    A modulus division by 10000 returns results between 0 and 9999. You add 1. Instead you want results between 1000 and 9999 so the range is less by 1000 and the offset is 1000. You learned the rule of proportion once upon a time ...
    – LotPings
    Aug 28, 2017 at 16:53
  • 4 digits ranging from 1000 to 9999, i.e. 9000 numbers. If you take mod by 3 then you get 0, 1, 2. Similarly, you take mod by 9000 and get 0 - 8999. Then add the initial val 1000 to get 1000 - 9999. Sep 1, 2017 at 4:20

4 Answers 4


Generate Random 4-Digit Number Windows Batch Script

You can use a batch script with Windows to complete this task easily as I'll explain below.

Essentially this will. . .

  1. Use PowerShell's Get-Random function to generate the random number
    • The random number value will be in a range of 0000-9999 using the -Maximum and -Minimum parameters
  2. Use the "0" Custom Specifier—"{0:0000} -f $fRandom" with PowerShell and .NET to ensure the random number value has leading zeros added ensuring it always has 4 digits

  3. Result in the PowerShell random number value being echo'd as the output

  4. Use a FOR /F loop to take the result of the executed PowerShell script (the random number) within it, and save that result as a variable to use in the batch script for your needs (%RAND%)

Batch Script

If the random number cannot be 4-digit values with prefixed zeros like 0001, 0011, or 0111 you can change the -Minimum 1000 like that instead but random digits 1-999 will be omitted so the value of 1000 is what you want to use for the -Minimum parameter if that's the case.


 SET PSScript=%Temp%\~tmpRandom4Digit.ps1
 IF EXIST "%PSScript%" DEL /Q /F "%PSScript%"

 ECHO $fRandom = Get-Random -Maximum 9999 -Minimum 0000>>"%PSScript%"
 ECHO $Random  = "{0:0000}" -f $fRandom>>"%PSScript%"
 ECHO ECHO $Random>>"%PSScript%"

 SET PowerShellDir=C:\Windows\System32\WindowsPowerShell\v1.0 
 CD /D "%PowerShellDir%" 
 FOR /F "DELIMS=" %%A IN ('Powershell -ExecutionPolicy Bypass -Command "& '%PSScript%'"') DO SET "RAND=%%A"

 echo %RAND%


Further Resources


A simple solution could be to take the last digit of four random generated numbers

set "RAND=%random:~-1%%random:~-1%%random:~-1%%random:~-1%"

Your could use my batch script:

@echo off
if "%1" == "/?" (
echo Usage:
echo random [min] [max]
echo random /?
echo Can be used to generate a random number between [min] - [max] and displays the output in the RDM variable.
echo./? can be used to display help
echo Examples:
echo random 1 5 - Generates a random number between 1 - 5
echo random /? - Displays help
if not "%1" == "/?" set /a rdm=(%random%*%2/32768)+%1

Then drag in the C:\Windows\System32 Folder and type "random /?" for the syntax.

  • Nice, worked for me. You can add an echo %rdm% to print the value
    – Deepak
    Nov 13, 2019 at 10:34

You just need to adjust the minimum value and the range on your initial solution.

@echo off
SET /A RAND=%RANDOM%%%9000+1000
echo %RAND%

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.