M BUZZ CRAZE NEWS
// news

Generate a random number with 4 digits only

By John Parsons

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

My Batch Script

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

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)

Question

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

3

4 Answers

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.

 @ECHO ON 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% EXIT

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.
echo random [min] [max]
echo random /?
echo.
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.
echo Examples:
echo.
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.

1

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

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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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