Q:

Generate password with minimum and maximum length : batch file

Trying to generate password with given characters but need to define minimum and maximum password string length inside batch file. 
i created batch file which is generating password string perfectly but i was unable to define length of string in Min_RNDLength & Max_RNDLength field. 
@Echo Off
Setlocal EnableDelayedExpansion
Set Min_RNDLength=8
Set Max_RNDLength=30
Set _Alphanumeric=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
Set _Str=%_Alphanumeric%987654321
:_LenLoop
IF NOT "%_Str:~18%"=="" SET _Str=%_Str:~9%& SET /A _Len+=9& GOTO :_LenLoop
SET _tmp=%_Str:~9,1%
SET /A _Len=_Len+_tmp
Set _count=0
SET _RndAlphaNum=
:_loop
Set /a _count+=1
SET _RND=%Random%
Set /A _RND=_RND%%%_Len%
SET _RndAlphaNum=!_RndAlphaNum!!_Alphanumeric:~%_RND%,1! 
If !_ count!  lss %_RNDLength% goto _loop
Echo Random string is !_ RndAlphaNum! 

not sure how to overcome this issue. 

A:

This is the way I would do it:
@Echo Off
Setlocal EnableDelayedExpansion
Set /A Min_RNDLength=8, Max_RNDLength=30
Set Alphanumeric=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789

rem Randomize internal random number (optional)
rem for /L %%i in (1,1,%time:~-1%) do set /A ! random! 

rem Get random lenght between limits:
set /A RNDLength=%Random% %% (Max_RNDLength-Min_RNDLength) + Min_RNDLength

Set "RndAlphaNum="
rem Generate such a number of random characters from Alphanumeric set (62 characters)
for /L %%i in (1,1,%RNDLength%) do (
   set /A RND=!random! %%  62
   for %%r in (! RND!)  do set "RndAlphaNum=!RndAlphaNum!!Alphanumeric:~%%r,1!" 
)

Echo Random string is %RndAlphaNum%

