如何在BATCH脚本中随机使用?

如何在BATCH脚本中随机使用?

%RANDOM%给你一个介于0和32767之间的随机数。

使用类似SET /A test=%RANDOM% * 100 / 32768 + 1的expression式,可以将范围更改为任何您喜欢的内容(这里的范围是[1 … 100],而不是[0 … 32767])。

%RANDOM%给你一个介于0和32767之间的随机数。

您可以通过以下方式控制号码的范围:

 set /a num=%random% %%100 

– 会产生0〜99之间的数字。

这个:

 set /a num=%random% %%100 +1 

– 会产生1〜100之间的数字。

你可能想要得到几个随机数,并且可能希望能够为每个数指定一个不同的范围,所以你应该定义一个函数。 在我的例子中,我从25到30生成号码call:rand 25 30 。 结果是在函数退出后的RAND_NUM

 @echo off & setlocal EnableDelayedExpansion for /L %%a in (1 1 10) do ( call:rand 25 30 echo !RAND_NUM! ) goto:EOF REM The script ends at the above goto:EOF. The following are functions. REM rand() REM Input: %1 is min, %2 is max. REM Output: RAND_NUM is set to a random number from min through max. :rand SET /A RAND_NUM=%RANDOM% * (%2 - %1 + 1) / 32768 + %1 goto:EOF 
 @echo off & setLocal EnableDelayedExpansion for /L %%a in (1 1 100) do ( echo !random! ) 
 set /a number=%random% %% [maximum]-[minimum] 

例如“

 set /a number=%random% %% 100-50 

将给出一个介于100和50之间的随机数。如果不在批处理脚本中使用该行,请务必只使用一个百分比符号作为操作数!

你可以这样做,这不需要EnableDelayedExpansion

 :choosenamea cls set /a choosemname= %random% if %choosemname% GTR %max% goto choosenameb if %choosemname% LSS %min% goto choosenameb goto gotnamenow 

其中max是你的最大值, min是你的最小值。 这是有效的,但如果你的范围太小,可能需要很多轮。

 @echo off title Professional Hacker color 02 :matrix echo %random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%% echo %random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%% echo %random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%% echo %random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%% echo %random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%% echo %random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%% echo %random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%% echo %random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%% goto matrix 

假设你想要一个数字1-5; 你可以使用以下内容:

  :LOOP set NUM=%random:~-1,1% if %NUM% GTR 5 ( goto LOOP ) goto NEXT 

或者你可以使用:〜1,1代替:〜-1,1。 :-1 -1是不需要的,但它大大减less了达到正确范围的时间。 假设你想要一个数字1-50,我们需要决定2位数和1位数。 使用:

  :LOOP set RAN1=%random:~-1,1% if %RAN1% GTR 5 ( goto 1 ) if %RAN1%==5 ( goto LOOP ) goto 2 :1 set NUM=%random:~-1,1% goto NEXT :2 set NUM=%random:~-1,2% goto NEXT 

您可以添加更多的algorithm来决定大范围,如1-1000。

 @(IF not "%1" == "max" (start /MAX cmd /Q /C %0 max&X)ELSE set C=1&set D=2&wmic process where name="cmd.exe" CALL setpriority "REALTIME">NUL)&CLS :Y title %random%6%random%%random%%random%%random%9%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%&color %D%&ECHO %random%%C%%random%%random%%random%%random%6%random%9%random%%random%%random%%random%%random%%random%%random%%random%%random%&(IF %C% EQU 46 (TIMEOUT /T 1 /NOBREAK>nul&set C=1&CLS&IF %D% EQU 9 (set D=1)ELSE set /AD=%D%+1)ELSE set /AC=%C%+1)&goto Y 

简化多个IF语句和大量的((()))

对于那些并不总是想要黑屏的人来说,完全是随机的。

 @(IF not "%1" == "max" (start /MAX cmd /Q /C %0 max&X)ELSE set A=0&set C=1&set V=A&wmic process where name="cmd.exe" CALL setpriority "REALTIME">NUL)&CLS :Y (IF %A% EQU 10 set A=A)&(IF %A% EQU 11 set A=B)&(IF %A% EQU 12 set A=C)&(IF %A% EQU 13 set A=D)&(IF %A% EQU 14 set A=E)&(IF %A% EQU 15 set A=F) (IF %V% EQU 10 set V=A)&(IF %V% EQU 11 set V=B)&(IF %V% EQU 12 set V=C)&(IF %V% EQU 13 set V=D)&(IF %V% EQU 14 set V=E)&(IF %V% EQU 15 set V=F) (IF %A% EQU %V% set A=0) title %A%%V%%random%6%random%%random%%random%%random%9%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%&color %A%%V%&ECHO %random%%C%%random%%random%%random%%random%6%random%9%random%%random%%random%%random%%random%%random%%random%%random%%random%&(IF %C% EQU 46 (TIMEOUT /T 1 /NOBREAK>nul&set C=1&CLS&SET /AA=%random% %%15 +1&SET /AV=%random% %%15 +1)ELSE set /AC=%C%+1)&goto Y 

这将改变屏幕的颜色也是随机的。

这里是我为你创build的一个例子,它应该显示一个对话框,要求你select一个数字1-10,这取决于你select的数字,它会生成一个随机数的例子给你命名的batch file。 如果你select“1”,那么你会得到一个随机的1位数字的例子。 如果你select“10”,那么你会得到一个随机的10位数字的例子。

 @echo off color f0 set /p "FileName= Enter Filename (Without Extension) : " echo @echo off >> %FileName%.bat echo File Created! pause cls :CommandLine set /p "calc= ~%ComputerName%: Enter a number to recieve the amount of random digits :" if %calc%==genrand_help goto GenerateRandomHelp if %calc%==1 echo echo %%RANDOM:~-1%% >> %FileName%.bat if %calc%==2 echo echo %%RANDOM:~-1%%%%RANDOM:~-1%% >> %FileName%.bat if %calc%==3 echo echo %%RANDOM:~-1%%%%RANDOM:~-1%%%RANDOM:~-1%% >> %FileName%.bat if %calc%==4 echo echo %%RANDOM:~-1%%%%RANDOM:~-1%%%%RANDOM:~-1%%%%RANDOM:~-1%% >> %FileName%.bat if %calc%==5 echo echo %%Random%% >> %FileName%.bat if %calc%==6 echo echo %%Random%%%%RANDOM:~-1%% >> %FileName%.bat if %calc%==7 echo echo %%Random%%%%RANDOM:~-1%%%%RANDOM:~-1%% >> %FileName%.bat if %calc%==8 echo echo %%Random%%%%RANDOM:~-1%%%%RANDOM:~-1%%%%RANDOM:~-1%% >> %FileName%.bat if %calc%==9 echo echo %%Random%%%%RANDOM:~-1%%%%RANDOM:~-1%%%%RANDOM:~-1%%%%RANDOM:~-1%% >> %FileName%.bat if %calc%==10 echo echo %%Random%%%%Random%% >> %FileName%.bat goto CommandLine 

而完全是随机的,完全没有秩序: SET /AV=%random% %%15 +1

 @(IF not "%1" == "max" (start /MAX cmd /Q /C %0 max&X)ELSE set C=1&set V=A&wmic process where name="cmd.exe" CALL setpriority "REALTIME">NUL)&CLS :Y (IF %V% EQU 10 set V=A)&(IF %V% EQU 11 set V=B)&(IF %V% EQU 12 set V=C)&(IF %V% EQU 13 set V=D)&(IF %V% EQU 14 set V=E)&(IF %V% EQU 15 set V=F) title %V%%random%6%random%%random%%random%%random%9%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%&color %V%&ECHO %random%%C%%random%%random%%random%%random%6%random%9%random%%random%%random%%random%%random%%random%%random%%random%%random%&(IF %C% EQU 46 (TIMEOUT /T 1 /NOBREAK>nul&set C=1&CLS&SET /AV=%random% %%15 +1)ELSE set /AC=%C%+1)&goto Y 

现在具有dos彩虹的所有颜色

 @(IF not "%1" == "max" (start /MAX cmd /Q /C %0 max&X) ELSE set C=1&set D=A&wmic process where name="cmd.exe" CALL setpriority "REALTIME">NUL)&CLS :Y set V=%D% (IF %V% EQU 10 set V=A) & (IF %V% EQU 11 set V=B) & (IF %V% EQU 12 set V=C) & (IF %V% EQU 13 set V=D) & (IF %V% EQU 14 set V=E) & (IF %V% EQU 15 set V=F) title %random%6%random%%random%%random%%random%9%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%&color %V%&ECHO %random%%C%%random%%random%%random%%random%6%random%9%random%%random%%random%%random%%random%%random%%random%%random%%random% &(IF %C% EQU 46 (TIMEOUT /T 1 /NOBREAK>nul&set C=1&CLS&IF %D% EQU 15 (set D=1)ELSE set /AD=%D%+1) ELSE set /AC=%C%+1)&goto Y