| 1. |
Solve : Random numbers? |
|
Answer» Hi friends. I'm slowly coming to grips with notepad++ 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. 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. |
|