Oddly enough, I did try that approach earlier and it returned a bad value however, I'm back-tracking my test runs in case of a human error on my part...
Will let you know when I have completely retested the code.
Main Topics
Browse All TopicsThe problem here is simple. The code is written lexically correct. The nested IF statements are logically correct. Before the interpreter attempts to run this code, it first tries to resolve variables including command line variables. Where comparisons are made against %2, for the program to reach that part of the code %2 would infact be defined but the interpreter appears to ignore this logical fact. Is there a way to force the interpreter to accept this code without detracting from the current style?
What the code does: Simply put, it generates a random number between a min value and a max value. If there are no parameters, min and max are set to 1 and 9 respectively. If two values are passed as parameters then min and max are set to %1 and %2. Tests are then performed to validate that %1 and %2 are in range as well as the order of %1 and %2. Really simple but an annoyingly frustrating oversight.
@echo off
if "%1"=="" (
set /a Min = 1 // There are no parameters so set default values
set /a Max = 9
) else (
if not "%2"=="" ( // %1 exists....
if /i %1 lss 1 exit /b -2 // %2 also exists so validate both %1 and %2....
if /i %1 gtr 9 exit /b -3
if /i %2 lss 1 exit /b -4
if /i %2 gtr 9 exit /b -5
if /i %1 gtr %2 exit /b -6
set /a Min = %1 // and if all is well, set min and max values
set /a Max = %2
) else (
exit /b -1 // %1 exists but %2 does not so no need to validate either
)
) // The rest of the code os okay
set /a num = %random% %% (%max% - %Min% + 1) + %Min%
echo %num% // This line will be removed after debugging is complete
exit /b %num%
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
So far, the following code appears to work:
@echo off
if "%1"=="" (
set /a RndYear = "%random% %% (%date:~6,4% - 1752 + 1) + 1752"
) else (
if not "%2"=="" (
if "%1" lss "1752" exit /b -31
if "%1" gtr "9999" exit /b -32
if "%2" lss "1752" exit /b -33
if "%2" gtr "9999" exit /b -34
if "%1" gtr "%2" exit /b -35
set /a RndYear = "%random% %% (%2 - %1 + 1) + %1"
) else (
exit /b -36
)
)
echo %RndYear%
exit /b %RndYear%
Hello AmazingTech....
DOS is full of surprises. I recently discovered we can also use IF DEFINED and IF NOT DEFINED with %1, %2 %3 etc. but who would have thought of enclosing arithmetic expressions in double-quotes let alone individual operands of a conditional arithmetic IF statement....
For the purpose of my application, I don't want to exit with an error code if %1 = %2. But well spotted anyway.
Business Accounts
Answer for Membership
by: dlb6597Posted on 2009-04-28 at 13:27:06ID: 24254774
will it work if you put quotes around your variables and the value you are comparing to in all your "if /i" statements?
LIke such:
if /i "%1" lss "1" exit /b -2