1.

Solve : Random numbers?

Answer»

Hi friends. I'm slowly coming to grips with notepad++
in a game im doing I want to get a random number between 1 and 100
so I use
set /a num=%random% * 100 + 1
I then want to use that number to goto various parts of the prog
echo number is %num%
01 - 20 goto part a
21 - 70 goto part b
71 - 95 goto part c
96 - 100 goto part d

01 - 20 I use if %num% LSS 21 goto part a

96 - 100 I use If %num% GTR 95 goto part d

how do I set the middle two (21 - 70 and 71 - 95)

Look forward to a reply

regards, Raggie









Code: [Select]if %num% gtr 20 if %num% lss 71 goto :partb
Code: [Select]set /a num=%random%*100/32768+1
set flag=0

if %num% LSS 20 set flag=1 && goto :a
if %num% LSS 70 if not "%flag%"=="1" set flag=1 && goto :b
if %num% LSS 95 if not "%flag%"=="1" set flag=1 && goto :c
if not "%flag%"=="1" goto :d
echo ERROR at random number 1-100 generation

you may not need %flag% at all, but it's never a bad idea to have fail safes.Thanks guys I will try these ASAP.Quote from: Lemonilla on October 11, 2013, 07:55:34 PM

you may not need %flag% at all, but it's never a bad idea to have fail safes.

This is what I believe is called "cargo cult programming"

http://en.wikipedia.org/wiki/Cargo_cult_programming


If X is Y {
If X is really Y {
$Do_Something
}
}


correct me if I'm wrong, but since batch is linear in its processing, you don't need anything to do that, you just need to sequence the IF statements correctly.

e.g.
Code: [Select]REM set num
set /a num=%random%*100/32768+1

REM argue num value
if '%num%' LSS '21' goto result
if '%num%' LSS '70' goto result
if '%num%' LSS '96' goto result
if '%num%' LSS 101 goto result
goto error

REM check result
:result
echo your random number is: %num%
pause > nul

REM just in case
:error
cls
echo there has been an error.

Since each IF is arguing "less than (as exact value)" and it has a GOTO as the result, so it will GOTO result if in range, otherwise go on to the next argument. If a value passes through <21, then it's bound to hit one of the following segments...

Anyway... not sure if that logic MADE sense... lolIn ESSENCE you're right - but you've forced an ascii compare in your code, and got a number out by one.

Based on your code, this kind of thing will work:

Code: [Select]if %num% LSS 21 goto resultA
if %num% LSS 71 goto resultB
if %num% LSS 96 goto resultC
if %num% LSS 101 goto resultDQuote from: foxidrive on October 16, 2013, 07:03:38 AM
In essence you're right - but you've forced an ascii compare in your code, and got a number out by one.

Based on your code, this kind of thing will work:

Code: [Select]if %num% LSS 21 goto resultA
if %num% LSS 71 goto resultB
if %num% LSS 96 goto resultC
if %num% LSS 101 goto resultD

Yeah, I just kinda spat that out as an example. but since the number will only progress to the next IF argument if it exceeds the current range, then there's not really much need to scale floors and ceilings by nesting the statements - it's just letting DOS do it's thing. LOL!

Anyway, it's always nice to make the script less verbose if POSSIBLE.


Discussion

No Comment Found