Problem with random number generator

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
uwu owo TwT
Posts: 1
Joined: 09 Aug 2019 14:07
Location: Ireland
Contact:

Problem with random number generator

#1 Post by uwu owo TwT » 09 Aug 2019 14:36

Hello everyone,

I am trying to make a random number generator. However, I get an error message. The batch file code is

Code: Select all

:PreparationQuestionsAndValidityCheck
set /p MIN="What do you want the minimum number to be? Any integer between 0 and 32767 is allowed. "
if %min% gtr 32767 (goto InvalidMinimumIsTooBig)
if %min% lss 0 (goto InvalidMinIsTooSmall)
if %min% % 1 neq 0 (goto InvalidMinimumIsNotAnInteger)
set /p MAX="What do you want the maximum number to be? Any integer between 0 and 32767 is allowed. "
if %max% gtr 32727 (goto InvalidMaximumIsTooBig)
if %max% lss 0 (goto InvalidMaximumIsTooSmall)
if %max% lss %min% (goto InvalidMaximumIsLessThanMinimum)
if %max% % 1 neq 0 (goto InvalidMaximumIsNotAnInteger)
set /p NTG="How many would you like to generate? "
if %ntg% % 1 neq 0 (goto InvalidNTGIsNotAnInteger)
set /a glc=0
echo Generated number(s):

:GenerationLoop
set /a glc=glc+1
if %glc%==%ntg% goto SuccessfulGeneration
set /a result=(%RANDOM%*%max%/32768)+%min%
echo %result%
goto GenerationLoop

:SuccessfulGeneration
echo Continue when you are ready to exit.
pause
echo Exiting in 3
timeout 1 /nobreak
echo Exiting in 2
timeout 1 /nobreak
echo Exiting in 1
timeout 1 /nobreak
exit

:InvalidMinimumIsTooBig
echo Your settings are invalid. The minimum value is greater than 32767.
echo The generator will exit in 15 seconds. You can skip the delay by pressing a key.
timeout 15
exit

:InvalidMinimumIsTooSmall
echo Your settings are invalid. The minimum value is less than 0.
echo The generator will exit in 15 seconds. You can skip the delay by pressing a key.
timeout 15
exit

:InvalidMinimumIsNotAnInteger
echo Your settings are invalid. The minimum value is not an integer.
echo The generator will exit in 15 seconds. You can skip the delay by pressing a key.
timeout 15
exit

:InvalidMaximumIsTooBig
echo Your settings are invalid. The maximum value is greater than 32767.
echo The generator will exit in 15 seconds. You can skip the delay by pressing a key.
timeout 15
exit

:InvalidMaximumIsTooSmall
echo Your settings are invalid. The maximum value is less than 0.
echo The generator will exit in 15 seconds. You can skip the delay by pressing a key.
timeout 15
exit

:InvalidMaximumIsLessThanMinimum
echo Your settings are invalid. The maximum value is less than the minimum value.
echo The generator will exit in 15 seconds. You can skip the delay by pressing a key.
timeout 15
exit

:InvalidMaximumIsNotAnInteger
echo Your settings are invalid. The maximum value is not an integer.
echo The generator will exit in 15 seconds. You can skip the delay by pressing a key.
timeout 15
exit

:InvalidNTGIsNotAnInteger
echo Your settings are invalid. The number of results to generate is not an integer.
echo The generator will exit in 15 seconds. You can skip the delay by pressing a key.
timeout 15
exit
The error message I get is

Code: Select all

C:\Users\<my user name>\Desktop>■:
The system cannot find the drive specified.
My OS is Windows 8.1 x64. I would greatly appreciate any help given.
Thanks,

uwu owo TwT

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Problem with random number generator

#2 Post by aGerman » 11 Aug 2019 07:55

If that is your entire code then it's already weird to begin with a label. But it may explain the behavior you're facing. In case you saved the code in a Unicode charset, a byte order mark might be misinterpreted as drive letter because the first character after the BOM is the colon prepending the PreparationQuestionsAndValidityCheck label in this case.
Make sure you saved the code ANSI-encoded. E.g. open your file in Windows Notepad, invoke the Save As dialog, choose ANSI in the Encoding dropdown field, and All Files in the File Type dropdown field.

Steffen

Eureka!
Posts: 137
Joined: 25 Jul 2019 18:25

Re: Problem with random number generator

#3 Post by Eureka! » 11 Aug 2019 08:34

Code: Select all

if %max% % 1 neq 0 (goto InvalidMaximumIsNotAnInteger)
If you want to do calculations in batch, use set /A ...

Code: Select all

set /a TEST=%max% %% 1
if %TEST% neq  0 (goto :InvalidMaximumIsNotAnInteger)
Further: you need to rethink this code:

Code: Select all

set /a result=(%RANDOM%*%max%/32768)+%min%
For example, if min=1000, max=1000 and RANDOM=1000, this will give you: 1030.

There are more issues, but I don't want to spoil your fun figuring things out and learning stuff.

Szecska
Posts: 17
Joined: 15 Aug 2019 15:29
Location: Hungary

Re: Problem with random number generator

#4 Post by Szecska » 15 Aug 2019 15:43

Your problem is the file encoding.

Cmd.exe can only read unicode characters and your file does not start with one.
(I know because i just run into the same problem today; this is the unicode LE encoding's symptom)
As @AGerman said if you reopen notepad and save with ANSI encoding, the problem is gone :)

Post Reply