This runs but slows down as vars increase-RandomHexGenerator
Posted: 02 Jun 2012 15:40
i had a need to create a random hex string generator and this batch file grew out of it. making one string is relatively quick but more can slow it down. this is probably due to my ??misuse?? of goto rather than call, yes?
or maybe calling %random% once for each character instead of the whole string at once?
it runs fine in default mode (1 1 4) but when doing (16 8
the routine slows down, and at (16 16 16) it is reduced to a crawl. what can be done to speed up the logic?
or maybe calling %random% once for each character instead of the whole string at once?
it runs fine in default mode (1 1 4) but when doing (16 8


Code: Select all
@echo off
echo.
setlocal EnableExtensions EnableDelayedExpansion
:askque3
set /p MaxLoop=How many times to repeat selection? [1-16] ^<1^>
echo.
if "%MaxLoop%"=="" set MaxLoop=1
if %MaxLoop% GTR 0 if %MaxLoop% LSS 17 goto askque2
echo.
echo %MaxLoop% not in the expected range [1-16]
echo.
goto askque3
:askque2
:: get the amount of hex characters to display per string
set /p byteCount=How many digits do you want to display per pair? [1-16] ^<4^>
echo.
if "%byteCount%"=="" set byteCount=4
if %byteCount% GTR 0 if %byteCount% LSS 17 goto askque1
echo.
echo %byteCount% not in the expected range [1-16]
echo.
goto askque2
:askque1
:: get the amount of %byteCount% byte pairs to be generated and displayed
set /p QtySets=How many sets of %byteCount% do you want to generate? [1-16] ^<4^>
echo.
if "%QtySets%"=="" set QtySets=4
if %QtySets% GTR 0 if %QtySets% LSS 17 goto maxloop
echo %QtySets% not in the expected range [1-16]
echo.
goto askque1
:: loop to create hex value; byteCount times QtySets
:: eg. 519B or 6C2B0A or 0F14A28CD
set maxcount=0
:maxloop
set /a maxcount=%maxcount% + 1
:continue
set outercount=0
:outrloop
set /a outercount=!outercount! + 1
set count=0
:innrloop
set /a count=!count! + 1
:: get return of random mod 16
set /a randval=%random% %% 16
:: make the A-F characters
if %randval% EQU 10 set randval=A
if %randval% EQU 11 set randval=B
if %randval% EQU 12 set randval=C
if %randval% EQU 13 set randval=D
if %randval% EQU 14 set randval=E
if %randval% EQU 15 set randval=F
if %randval% EQU 16 set randval=0
:: sets the nth character of randval
set randval_!count!=%randval%
:: repeat randval byteCount times
if !count!==%byteCount% goto innrend
goto innrloop
:innrend
set Rand!outercount!=%randval_1%%randval_2%%randval_3%%randval_4%%randval_5%%randval_6%%randval_7%%randval_8%%randval_9%%randval_10%%randval_11%%randval_12%%randval_13%%randval_14%%randval_15%%randval_16%
:: repeat QtySets times
if !outercount!==%QtySets% goto outerend
goto outrloop
:outerend
:: now display the %byteCount% byte string
set Result!maxcount!=%Rand1% %Rand2% %Rand3% %Rand4% %Rand5% %Rand6% %Rand7% %Rand8% %Rand9% %Rand10% %Rand11% %Rand12% %Rand13% %Rand14% %Rand15% %Rand16%
:: now suppress trailing spaces
for /f "tokens=1-%byteCount% delims= " %%a in ("!Result%maxcount%!") do (
if "!Result%maxcount%:~-1!"==" " set Result%maxcount%=!Result%maxcount%: =!
:: show on screen
echo %%^Result!maxcount!%%^ = !Result%maxcount%!
)
:: check if maxcount is equal to maxloop, if yes end, if not goto maxloop
if %maxcount% GEQ %MaxLoop% goto :end
goto maxloop
:end
echo.
endlocal