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

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

250367 次浏览
@echo off & setLocal EnableDelayedExpansion


for /L %%a in (1 1 100) do (
echo !random!
)

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

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

您可能希望获得几个随机数,并希望能够为每个数指定不同的范围,因此应该定义一个函数。在我的示例中,我使用 call:rand 25 30生成从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

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

您可以通过以下方式控制数字的范围:

set /a num=%random% %%100

将产生0-99之间的数字。

这个:

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

将产生数量之间的1至100。

set /a number=%random% %% [maximum]-[minimum]

例子」

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

会给出一个100到50之间的随机数。如果您没有在批处理脚本中使用该行,请确保只使用一个百分比符号作为操作数!

@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

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

:choosenamea
cls
set /a choosemname=%random%


if %choosemname% GTR %max% goto choosenameb
if %choosemname% LSS %min% goto choosenameb
goto gotnamenow

其中 max是最大值,min是最小值。这是不是非常有效,因为可能需要很多轮,如果你的射程太小。此外,这对于大于32767的数字也不起作用。

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

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

或者您可以使用: ~ 1,1代替: ~-1,1。不需要 ~-1,1,但是它大大减少了达到正确范围所需的时间。假设你想要一个数字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

您可以向该算法添加更多内容,以确定较大的范围,如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 /A D=%D%+1)ELSE set /A C=%C%+1)&goto Y

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

现在呈现出双色彩虹的所有颜色

@(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 /A D=%D%+1)
ELSE set /A C=%C%+1)&goto Y

只是为了完全随机,完全缺乏秩序: SET /A V=%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 /A V=%random% %%15 +1)ELSE set /A C=%C%+1)&goto Y

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

@(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 /A A=%random% %%15 +1&SET /A V=%random% %%15 +1)ELSE set /A C=%C%+1)&goto Y

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

下面是我为你创建的一个例子,它应该显示一个对话框,要求你选择一个数字1-10,根据你选择的数字,它将生成一个随机数例子到一个批处理文件,你的名字。如果你选择“1”,那么你会得到一个随机1位数字的例子。如果你选择“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

如果你将除以一些较大的值,你将得到一个又一个巨大数量的重复。 您需要做的是取% RANDOM% 值的模:

@echo off
REM
SET maxvalue=10
SET minvalue=1


SETLOCAL
SET /A tmpRandom=((%RANDOM%)%%(%maxvalue%))+(%minvalue%)
echo "Tmp random: %tmpRandom%"
echo "Random:  %RANDOM%"
ENDLOCAL