0

Im using this batch for random test1-test3 without duplicate but not working

@echo off
setlocal EnableExtensions
setlocal EnableDelayedExpansion
set /A "RND_TOTAL=3, FLAG_DUP=0"
set /A "RND_MIN=1, RND_MAX=3, RND_INTER=1"
for /L %%I in (1,1,%RND_TOTAL%) do (
    call :SUB %%I
    SET /A R=!RND_NUM[%%I]!
    SET LINE[1]=TEST1
    SET LINE[2]=TEST2
    SET LINE[3]=TEST2
    echo !LINE[%R%]!
    pause
)
endlocal
exit /B
:SUB
set /A "RND_COUNT=%1-1"
:LOOP
set /A "RND_NUM[%1]=!RANDOM!%%((RND_MAX-RND_MIN)/RND_INTER+1)*RND_INTER+RND_MIN"
if %FLAG_DUP% EQU 0 (
    for /L %%I in (1,1,%RND_COUNT%) do (
        if !RND_NUM[%1]! EQU !RND_NUM[%%I]! (
            goto :LOOP
        )
    )
)
exit /B

I get the following output:

echo off

echo off

echo off
Compo
  • 36,585
  • 5
  • 27
  • 39
  • 2
    `echo off` means the variable in the `echo` line is empty. And don't use `setlocal EnableExtensions & setlocal EnableDelayedExpansion` on separate lines because that'll set up 2 different local environments. Call `setlocal EnableExtensions EnableDelayedExpansion` instead – phuclv Aug 21 '21 at 12:01
  • You are defining `R` in the same block you are trying to expand its content, you will therefore need to incorporate another level of delayed expansion. i.e. `%R%` cannot be used you'd need to use `!R!`, but you cannot use `!LINE[!R!]!`, so need that additional level. – Compo Aug 21 '21 at 13:12
  • Change `echo !LINE[%R%]!` to `for %%J in (!R!) do echo !LINE[%%J]!` (preferred) or to `call echo %%LINE[!R!]%%`. Refer also to [Arrays, linked lists and other data structures in cmd.exe (batch) script](https://stackoverflow.com/q/10166386)… – aschipfl Aug 21 '21 at 16:36

0 Answers0