Ever wondered how we can generate random numbers in a batch file. Yes, I am talking about our good old faithful friend ‘.bat’ file. Why do I need one? You may ask! I had to generate a unique filename everytime because I had to backup a folder before I updated it.
So tell us is there one? Yes use ‘random’, it’s a built in command which generates a random number when you invoke it. Every time a unique one! So if you type in ‘random’ at a command line and then press enter, you’ll get an error because it’s not an independent executable, but a feature that is built into command prompt to help generate random numbers. So how will I get a random number? This is how I do it…
echo %random%
Make a random directory….
mkdir C:\Nibu_%random%
If Command Extensions are enabled, then there are several dynamic environment variables that can be expanded but which don’t show up in the list of variables displayed by SET. These variable values are
computed dynamically each time the value of the variable is expanded. If the user explicitly defines a variable with one of these names, then that definition will override dynamic ones like %random%.
For more information type “set /?”
To enable command extensions open up regedit and then navigate to
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor\EnableExtensions
or
HKEY_CURRENT_USER\Software\Microsoft\Command Processor\EnableExtensions
and set value to 1. If not present then add one REG_DWORD value with an identical name and value set to 1.
Nice blog. Good stuff here
Thanks Anand.
That will give us random numbers from 1 to 32768.
What if we want random numbers from 1 to 20?
Use the modulus:
set /a var = %random% %% 20 + 1
maybe….
set /a (%random%/1638)+1
Divide 32768 by whatever number of choices you need, then use that number with the random variable, adding 1 so that it rounds up to your highest numbered choice.
——–
@echo off
cls
echo -
set /p NumOfChoices=Enter number of choices…
echo – -
set /a Divisor=32768/NumOfChoices
echo – - -
echo – Ready to start spewing random numbers as soon as you
echo -
pause
:loop
title Ctrl-C to stop this batch file
set /a ThaRandomNum=(%random%/%Divisor%) + 1
echo %ThaRandomNum%
goto loop
Thanks, helped me out in one of my projects.
Nice to know that anil
echo off
cls
echo -
set /p NumOfChoices=Enter number of choices…
echo – -
set /a Divisor=%NumOfChoices%/2
echo %NumOfChoices%
echo – – -
echo – Ready to start spewing random numbers as soon as you
echo -
pause
:loop
title Ctrl-C to stop this batch file
set /a randoom=%Random%
set /a ThaRandomNum1=%randoom%/%Divisor%
set /a ThaRandomNum2=%ThaRandomNum1%+1
echo %ThaRandomNum2%
goto loop
I love how this doesn’t work in a for loop:
for %%i in (*.*) do (echo %random%
)
wtf?
You need to use delayed expansion.
setlocal enabledelayedexpansionfor %%i in (*) do (
echo !random!
)
Thanks guys, this helped me a lot
For anyone who still needs it, here is the best way to “control” your random numbers..
set rand=%random:~-n%
where ‘n’ is the amount of digits you want your random number to be. eg:
set rand=%random:~-2%
will randomly generate a 2 digit number.
Hope this helps
WOW, thanks for that hint!!!!! Really, that saved a lot of hassle. (I didn’t even know there is something that “sophisticated” in windows batch processing possible)
So the code %random:~-”# here”% will generate a code with the “# here” many digits. If I wanted it to be a unique 4 digit number (no repeats of the same integer like 1123 or 1111 or 1113) how would I do that, and also is it possible that CMD already gives you a unique digit if the # of digits is <10?
So what if I want a random # 1-10 so this will function
“Random # gen thing script here”
set /p input=Enter:
if “%input%”=”1″ goto directory1
if “%input%”=”2″ goto directory2
if “%input%”=”3″ goto directory3
if “%input%”=”4″ goto directory4
if “%input%”=”5″ goto directory5
if “%input%”=”6″ goto directory6
if “%input%”=”7″ goto directory7
if “%input%”=”8″ goto directory8
if “%input%”=”9″ goto directory9
if “%input%”=”10″ goto directory10