0
@echo off
echo lol testing
pause
@echo off
Setlocal EnableDelayedExpansion
Set _RNDLength=8
Set _Alphanumeric=0123456789ABCDEF
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=
SET :_loop
:_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

Set _RNDZLength=4
SET _RNDZ=%Random%
SET _RndAlphaNumz=
:_loop
SET _RndAlphaNumz=!_RndAlphaNumz!!_Alphanumeric:~%_RNDZ%,1!
If !_count! lss %_RNDZLength% goto _loop

Set _RNDZALength=4
SET _RNDZA=%Random%
SET _RndAlphaNumez=!_RndAlphaNumez!!_Alphanumeric:~%_RNDZA%,1!
If !_count! lss %_RNDZALength% goto _loop

Set _RNDZADLength=4
SET _RNDZAD=%Random%
SET _RndAlphaNumedz=!_RndAlphaNumedz!!_Alphanumeric:~%_RNDZAD%,1!
If !_count! lss %_RNDZALength% goto _loop

Set _RNDZADALength=12
SET _RNDZADA=%Random%
SET _RndAlphaNumedaz=!_RndAlphaNumedaz!!_Alphanumeric:~%_RNDZADA%,1!
If !_count! lss %_RNDZALength% goto _loop

echo !_RndAlphaNum!-!_RndAlphaNumz!
pause

this is my code (i am aware that only 2 variables are in the echo) i am trying to make it print 8 - 4 - 4 - 12 tried a lot of things could anyone help? i copied the code from the 4-4 generator i made but i changed it from 4 to 8 been on this for like 2 hours trying to figure out whats wrong but have not found

  • Two identical labels are guaranteed to cause trouble (if you don't know exactly how they work and take advantage of that knowledge). Also, line 15 (`SET :_loop`) doesn't look quite right. – Stephan May 21 '22 at 19:37

1 Answers1

0

This seems like an XY problem to me. If you're trying to generate a GUID, there are probably easier ways.


But if you prefer to reinvent the wheel, then I suggest using a Batch function to generate your randomness would be a much cleaner solution than your chutes-and-ladders code. Something like this:

@echo off & setlocal disabledelayedexpansion

call :rndhex 8 group[0]
call :rndhex 4 group[1]
call :rndhex 4 group[2]
call :rndhex 4 group[3]
call :rndhex 12 group[4]

echo %group[0]%-%group[1]%-%group[2]%-%group[3]%-%group[4]%

exit /b

:rndhex <length> <return_var>
setlocal enabledelayedexpansion
for %%I in (
    "0=0" "1=1" "2=2" "3=3" "4=4" "5=5" "6=6" "7=7" "8=8"
    "9=9" "10=A" "11=B" "12=C" "13=D" "14=E" "15=F"
) do set %%I

for /L %%I in (1,1,%~1) do (
    set /a "digit = !random! * 16 / 32768"
    for %%A in (!digit!) do set "hex=!%%A!!hex!"
)

endlocal & set "%~2=%hex%"
exit /b
rojo
  • 24,000
  • 5
  • 55
  • 101