

InterviewSolution
Saved Bookmarks
1. |
Solve : Random number 40-90? |
Answer» <html><body><p>I have been looking for a batch who random chose a number 40-90, but I can't find any command who can specify a certain number. Why I want 40-90 is because I <a href="https://interviewquestions.tuteehub.com/tag/use-241643" style="font-weight:bold;" target="_blank" title="Click to know more about USE">USE</a> 1-25 already and the 25-39 I made <a href="https://interviewquestions.tuteehub.com/tag/space-239477" style="font-weight:bold;" target="_blank" title="Click to know more about SPACE">SPACE</a> if I'm going to use more numbers later.<br/><br/>Here is how I was thinking it should look like<br/><br/> Code: <a>[Select]</a>echo off<br/>cls<br/><br/>Echo Text<br/><br/>//The random number goes here.<br/><br/>If '40' Goto '40'<br/>If '41' Goto '41'<br/><br/>//Here would the random number from 40-90 go.<br/>:40<br/>echo Text<br/>GOTO start<br/><br/>:41<br/>echo Text<br/>GOTO start<br/><br/>:start<br/>set /p var=<br/><br/>I'm still learning how to use all batch commands, I just know the basics of it, but I would be grateful if anyone have the knowledge how can help me.I found this in the snippet closet. Just a few tweaks and it is made to order:<br/><br/> Code: <a>[Select]</a>echo off<br/>setlocal<br/><br/>set intLowNumber=40<br/>set intHighNumber=90<br/><br/>set /a rnd=%random% %% (%intHighNumber% - %intLowNumber% + 1) + %intLowNumber%<br/>echo Random number between %intLowNumber% and %intHighNumber% is %rnd% <br/><br/>FYI: use <strong>rem</strong> statements for comments; // are <a href="https://interviewquestions.tuteehub.com/tag/used-763273" style="font-weight:bold;" target="_blank" title="Click to know more about USED">USED</a> in other languages, not batch<br/><br/>Good luck. <br/>Thanks for the help, but I got to a other bummer I'm trying to get that the random number it create it then will go to a number,<br/><br/> Code: <a>[Select]</a>echo off<br/>cls<br/>setlocal<br/><br/>set intLowNumber=40<br/>set intHighNumber=90<br/><br/>set /a rnd=%random% %% (%intHighNumber% - %intLowNumber% + 1) + %intLowNumber%<br/>%rnd% ((<=If that number is 40 then)) GOTO 40<br/>%rnd% ((<=If that number is 41 then)) GOTO 41<br/><br/>But every time I try to do that I get "GOTO was unaccepted at this time" and then it kill it self, I have tried different commands but it still wont work...have you created the <a href="https://interviewquestions.tuteehub.com/tag/labels-239710" style="font-weight:bold;" target="_blank" title="Click to know more about LABELS">LABELS</a> :40 and :41 for it to jump to ? Quote from: gpl on August 04, 2010, 04:12:19 AM</p><blockquote>have you created the labels :40 and :41 for it to jump to ?<br/></blockquote> <br/>yes, I also have 40-90 Code: <a>[Select]</a>%rnd% ((<=If that number is 40 then)) GOTO 40<br/>%rnd% ((<=If that number is 41 then)) GOTO 41<br/><br/>Is this really part of your "code", Frejoh466? If so, I believe I know why your script keeps crashing.<br/> Quote from: Frejoh466 on August 03, 2010, 01:51:22 PM<blockquote>I'm still learning how to use all batch commands, I just know the basics of it, but I would be grateful if anyone have the knowledge how can help me.<br/></blockquote> <br/>since you have only just started, why not learn a programming language? A good programming language such as Python (and others) provides you programming concepts and constructs, such as arrays, hashes, dictionaries that every programmer should know and many more. Plus, it comes with modules/libraries that make programming easier and fun. Here's one way<br/>to get random number in <a href="https://docs.python.org">Python</a>.<br/><br/> Code: <a>[Select]</a>import random<br/>random_number=random.randrange(100)<br/>if random_number < 40:<br/> print "do something"<br/>elif random_number<90:<br/> print "do something" <br/><br/><br/> Quote from: Salmon <a href="https://interviewquestions.tuteehub.com/tag/trout-661901" style="font-weight:bold;" target="_blank" title="Click to know more about TROUT">TROUT</a> on August 04, 2010, 04:46:41 AM<blockquote> Code: <a>[Select]</a>%rnd% ((<=If that number is 40 then)) GOTO 40<br/>%rnd% ((<=If that number is 41 then)) GOTO 41<br/><br/>Is this really part of your "code", Frejoh466? If so, I believe I know why your script keeps crashing.<br/><br/></blockquote> <br/>That is not what my code look like, my code is kinda long so I only use the part that don't work so it wont be that long...<br/><br/>I'm trying to use that code in this part,<br/><br/> Code: <a>[Select]</a>if EXIST "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" goto unlock<br/>if NOT EXIST Hide goto create<br/>:lock<br/>ren Hide "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"<br/>attrib +h +s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"<br/>goto End<br/>:unlock<br/>echo Enter password to Unlock folder<br/>set /p "pass=>"<br/>if NOT %pass%==lol goto exit<br/>attrib -h -s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"<br/>ren "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" Hide<br/>goto exit<br/>:create<br/>md Hide<br/>goto exit<br/><br/>:exit<br/>exit<br/><br/>note quote marks<br/><br/> Code: <a>[Select]</a>if NOT "%pass%"=="lol" goto exit Quote from: Salmon Trout on August 04, 2010, 05:22:48 AM<blockquote>note quote marks<br/><br/> Code: <a>[Select]</a>if NOT "%pass%"=="lol" goto exit</blockquote> <br/>But it work without the quotes. Quote from: Frejoh466 on August 04, 2010, 06:40:44 AM<blockquote>But it work without the quotes.<br/></blockquote> <br/>Try pressing ENTER when it asks for the password.<br/>Frejoh466, you changed scripts! First it was all that "goto 40" stuff, then all of a sudden you're trying to hide Control Panel! What's going on?<br/>What do you think this line is going to do? (Line 6)<br/><br/> Code: <a>[Select]</a>goto End Quote from: Salmon Trout on August 04, 2010, 07:18:42 AM<blockquote>Frejoh466, you changed scripts! First it was all that "goto 40" stuff, then all of a sudden you're trying to hide Control Panel! What's going on?<br/><br/></blockquote> <br/>And this is why I only post the part that don't work... Ok forget my last post, I'm trying to use that random number the %rnd% create to go to the :40 or :41 etc. <br/><br/>so the number that %rnd% create it will goto<br/><br/>Hope this get through...Whatever it is you are trying to do, I am quite sure that there is a better way. Probably involving fewer lines! The computed goto is heavily frowned upon by programming experts. However if that is what you want...<br/><br/>(You can change intHighNumber and put in the label sections from 51 to 90 yourself. Notice that you do not need goto end after the last number because you are going there anyway.)<br/><br/> Code: <a>[Select]</a>echo off<br/>set intLowNumber=40<br/>set intHighNumber=50<br/>set /a rnd=%random% %% (%intHighNumber% - %intLowNumber% + 1) + %intLowNumber%<br/>goto %rnd%<br/><br/>:40<br/>echo number=40<br/>goto end<br/>:41<br/>echo number=41<br/>goto end<br/>:42<br/>echo number=42<br/>goto end<br/>:43<br/>echo number=43<br/>goto end<br/>:44<br/>echo number=44<br/>goto end<br/>:45<br/>echo number=45<br/>goto end<br/>:46<br/>echo number=46<br/>goto end<br/>:47<br/>echo number=47<br/>goto end<br/>:48<br/>echo number=48<br/>goto end<br/>:49<br/>echo number=49<br/>goto end<br/>:50<br/>echo number=50<br/><br/>:end<br/></body></html> | |