我正在尝试编写一个 Windows 批处理文件,该文件将生成一个从 0 到 9 的随机数,然后根据随机数在我们的游戏服务器中加载不同的地图列表。
我试图修改此论坛上用于生成随机字符的类似文件,但是当我将 maxchars 变量的长度减少到 1 时。
我有时会得到@echo is off
回应,有时会得到一个数字。
这是我所拥有的:
@echo off & setlocal EnableDelayedExpansion
REM Random.bat
REM
REM Change these values to whatever you want, or change the code to take them
REM as command-line arguments. You must set CHARS_LEN to the string length
REM of the string in the CHARS variable.
REM
REM This script generates a string of these characters at least
REM MIN_CHARS_IN_LINE chars long and at most MAX_CHARS_IN_LINE chars long.
SET CHARS=0123456789
SET /A CHARS_LEN=10 + 10 + 10
SET /A MIN_CHARS_IN_LINE=1
SET /A MAX_CHARS_IN_LINE=2
REM Pick a random line length and output a random character until we reach that
REM length.
call:rand %MIN_CHARS_IN_LINE% %MAX_CHARS_IN_LINE%
SET /A LINE_LENGTH=%RAND_NUM%
SET LINE=
for /L %%a in (1 1 %LINE_LENGTH%) do (
call:rand 1 %CHARS_LEN%
SET /A CHAR_INDEX=!RAND_NUM! - 1
CALL SET EXTRACTED_CHAR=%%CHARS:~!CHAR_INDEX!,1%%
SET LINE=!LINE!!EXTRACTED_CHAR!
)
echo !LINE!
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
:eof
一旦我能够可靠地选择字符,我只需在最后添加一个选择过程,该过程将为每个地图列表使用不同的命令行调用服务器。