Page 1 of 1

Problem with random number generator

Posted: 09 Aug 2019 14:36
by uwu owo TwT
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

Re: Problem with random number generator

Posted: 11 Aug 2019 07:55
by aGerman
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

Re: Problem with random number generator

Posted: 11 Aug 2019 08:34
by Eureka!

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.

Re: Problem with random number generator

Posted: 15 Aug 2019 15:43
by Szecska
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 :)