Generieren einer Zufallszahl von 0-9 in einer Windows-batch-Datei

Ich bin beim schreiben einer Windows-batch-Datei, die generiert eine zufällige Zahl von 0 bis 9, und laden Sie dann eine andere map-Liste in unserer game-server, basierend auf der Zufallszahl.

Habe ich versucht zu ändern, eine ähnliche Datei, die wurde in diesem forum für die Generierung von zufälligen Zeichen, aber wenn ich reduzieren die maxchars-Variablen um 1 in der Länge.

Manchmal bekomme ich @echo is off als Antwort und manchmal eine Zahl.

Hier ist, was ich habe:

@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

Einmal kann ich das zuverlässig die Auswahl an Zeichen die ich nur hinzufügen müssen, um ein Auswahlverfahren zu Ende, die rufen den server mit einer anderen Befehlszeile für jede Karte Liste.

  • Ich glaube, du verpasst das "@echo off" das echo ausgeschaltet und die Ausgabe des Befehls zeigt
InformationsquelleAutor Kai Boulton | 2012-07-26
Schreibe einen Kommentar