How to generate a list of random numbers without duplicates in pure batch scripting?

前端 未结 5 1952
青春惊慌失措
青春惊慌失措 2020-12-07 03:51

I want to generate a list of random numbers with a predefined number of items %RND_TOTAL% in the given range from %RND_MIN% to %RND_MAX%

5条回答
  •  猫巷女王i
    2020-12-07 04:14

    replace

    for /L %%I in (1,1,%RND_TOTAL%) do (
        rem compute a random number:
        set /A "RND_NUM[%%I]=!RANDOM!%%((RND_MAX-RND_MIN)/RND_INTER+1)*RND_INTER+RND_MIN"
        echo !RND_NUM[%%I]!
    )
    

    with

    for /L %%I in (%rnd_min%,1,%RND_max%) do set "rnd_num{%%I}="
    set /a rnd_count=rnd_total
    :rnd_loop
    rem compute a random number:
    set /A "RND_selection=%RANDOM%%%((RND_MAX-RND_MIN)/RND_INTER+1)*RND_INTER+RND_MIN"
    if not defined rnd_num{%rnd_selection%} (
     SET "rnd_num{%rnd_selection%}=Y"
     set /a rnd_num[%count%]=rnd_selection
     echo %rnd_selection%
     set /a rnd_count-=1
    )
    if %rnd_count% neq 0 goto rnd_loop
    

    Each time a selection is made, rnd_num{selectionmade} is set to Y so if it is selected again, it is defined and the recording/output/count-1-less is skipped.

提交回复
热议问题