如何在BATCH脚本中使用随机?
如何在BATCH脚本中使用随机?
Answers:
%RANDOM%
为您提供0到32767之间的随机数。
使用类似的表达式SET /A test=%RANDOM% * 100 / 32768 + 1
,您可以将范围更改为任意值(此处的范围是[1…100]而不是[0…32767])。
SETLOCAL ENABLEDELAYEDEXPANSION
和使用!RANDOM!
而不是%RANDOM%
,例如Eugene posted。
%RANDOM% * (%maxval% - %minval% + 1) / 32768 + %minval%
; 只需先设置minval
并设置maxval
为适当的值即可。
%RANDOM%为您提供0到32767之间的随机数。
您可以使用以下方法控制数字范围:
set /a num=%random% %%100
-将产生介于0到99之间的数字。
这个:
set /a num=%random% %%100 +1
-将产生1〜100之间的数字。
%random% %%100
不是魔术语法,实际上实际上%random % %% 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!
)
假设您要输入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)之间进行决定。
goto
当您可以使用上述一种衬管时,为什么还要使用?其他解决方案可让您显式指定范围。您的代码有什么优势?
@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
@(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 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
现在具有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 /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