This runs but slows down as vars increase-RandomHexGenerator

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
timbertuck
Posts: 76
Joined: 21 Dec 2011 14:21

This runs but slows down as vars increase-RandomHexGenerator

#1 Post by timbertuck » 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 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? :o

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

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: This runs but slows down as vars increase-RandomHexGener

#2 Post by Aacini » 02 Jun 2012 16:07

Excuse me. I am too lazy to read your code. Perhaps if you explain in a couple lines what you want and what the three parameters mean with some output examples I may write a faster code that do the same thing.

EDIT: Perhaps is this what you want?

Code: Select all

set hexDigit=0123456789ABCDEF


:maxloop
set Result=
for /L %%m in (1,1,%MaxLoop%) do (
   set outter=
   for /L %%o in (1,1,%QtySets%) do (
      set inner=
      for /L %%i in (1,1,%byteCount%) do (
         :: get return of random mod 16
         set /A randval=!random! %% 16
         :: assemble the inner string of hex digits
         for %%h in (!randval!) do set inner=!inner!!hexDigit:~%%h,1!
      )
      set outter=!outter!!inner!
   )
   set Result=!Result! !outter!
)

:end


Antonio

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: This runs but slows down as vars increase-RandomHexGener

#3 Post by dbenham » 02 Jun 2012 16:55

timbertumck - your set maxcount=0 above :maxloop was never getting executed.

I think this is very similar to what Aacini posted, but I already had this written so decided to post anyway. Leaving the top portion of the code the same and only changing from :maxloop down.

Code: Select all

:maxloop
set hex=0123456789ABCDEF
for /l %%A in (1 1 %MaxLoop%) do (
  set "result%%A="
  for /l %%B in (1 1 %QtySets%) do (
    for /l %%C in (1 1 %byteCount%) do (
      set /a randval=!random! %% 16
      for /f %%N in ("!randval!") do set "result%%A=!result%%A!!hex:~%%N,1!"
    )
    set "result%%A=!result%%A! "
  )
  set "result%%A=!result%%A:~0,-1!"
  echo Result%%A = !Result%%A!
)


Dave Benham

Post Reply