frexed
Goto Top

Batch Zufallszahl begrenzen!

Hallo Leute,


Ich möchte in folgendem Code ein Problem lösen. (Eine Zufallszahl begrenzen, z.B.: Die Kommandozeile soll eine Zufallzahl wischen 8 und 17 ausgeben) Es klappt bei mir irgendwie nicht, habt ihr eine Lösung ?


@echo off

color a

title Zufallsgenerator

echo Zufallsgenerator

echo.

echo 1) Starten

echo 2) Verlassen

echo.

set /p Option= Deine Option:

if %Option% == 1 ( goto 1 )

if %Option% == 2 ( goto 2 )

:Error

cls

echo Ein Fehler ist aufgetreten !

pause>NUL

exit

{{comment_single_line_double_colon:1}}

exit

:1

cls

echo Gib die minimale Zahl an!

echo.

set /p Option1= Deine minimale Zahl:

cls

echo Gib die maximale Zahl an!

echo.

set /p Option2= Deine maximale Zahl:

cls

:again

set /a Option2 += 1

set /a Zufallszahl=%random% %%Option1%+%%Option2%

if %Zufallszahl% == 0 ( set /a Z += 1 )

echo Deine Zufallszahl: %Z% !

pause>NUL

goto 1


Das Problem ist, dass er ständig die Fehlermeldung "Division durch 0" anzeigt. face-sad

Habt ihr eine Idee, die mir helfen könnte ? Danke im Voraus !


Grüße Frexed

Content-Key: 288542

Url: https://administrator.de/contentid/288542

Printed on: April 25, 2024 at 06:04 o'clock

Mitglied: 122990
Solution 122990 Nov 16, 2015 updated at 17:23:13 (UTC)
Goto Top
Moin.
@echo off
set /a min=8
set /a max=17
set /a rnd=%random% * (%max% - %min% + 1) / 32768 + %min%
echo %rnd%
Für das mehrfache Generieren von Zahlen einfach das ganze in eine Subroutine packen
@echo off & setlocal enabledelayedexpansion
set /a min=8
set /a max=17

echo Generiere 10 Zufallszahlen im Bereich %min%-%max%:

for /L %%a in (1 1 10) do @(
	call :rand %min% %max%
	echo !rnd!
)
goto :eof

:rand
set /a rnd=%random% * (%2 - %1 + 1) / 32768 + %1
goto :eof
Gruß grexit
Member: Frexed
Frexed Nov 16, 2015 updated at 17:23:41 (UTC)
Goto Top
Danke für deine Hilfe grexit !

Jetzt verstehe ich alles ! face-smile

Grüße Frexed
Member: o0Julia0o
o0Julia0o Nov 29, 2023 at 23:16:21 (UTC)
Goto Top
Also wenn ich das hier ausführe, komm stets 11 bei herum:
@echo off
set /a min=8
set /a max=17
set /a rnd=%random% * (%max% - %min% + 1) / 32768 + %min%
echo %rnd%

Zufall?