| 1. |
Solve : A randomizer??? |
|
Answer» Is it possible to make a batch file that gives you random answers? Like if you wanted to choose your lottery #'s or something you could put in all the possible #'s and it gives you a random ONE every time? Thanks, it worked!I was just thinking; is there maybe a way to have a group of variables? Then you could make show a random variable from that group? Or is there another way?Yes, you can do this with months or anything else by setting up an array. Code: [Select]@echo off set array=JanFebMarAprMayJunJulAugSepOctNovDec :loop set /a num=%random%/2730 if %num%==0 goto loop call set /a disp=3*%num%-3 call set mon=%%array:~%disp%,3%% echo %mon% goto loop This EXAMPLE has no exit; use CTL-C. Quote Or is there another way?Yes, learn Windows scripting...always use the right tools for the job. Batch is a command language not a programming language. 8-)Everytime I click on it, it says april! And I deleted the "goto loop" and changed it to pause!There are two goto loop statements. In any CASE, the example works as advertised. You break it, you buy it. Quote Is it possible to make a batch file that gives you random answers? Like if you wanted to choose your lottery #'s or something you could put in all the possible #'s and it gives you a random one every time? In Python, Code: [Select]>>> import random >>> random.randrange(1,100) #assume lottery NUMS range from 1,100 50 In Perl Code: [Select]print int(rand(100)); In windows scripting, an eg here http://www.microsoft.com/technet/scriptcenter/resources/qanda/may05/hey0518.mspx In batch, yes, you can do that in batch too, if you are bored. This is what I changed it too: Code: [Select]@echo off set array=JanFebMarAprMayJunJulAugSepOctNovDec :loop set /a num=%random%/2730 if %num%==0 goto loop call set /a disp=3*%num%-3 call set mon=%%array:~%disp%,[highlight]3[/highlight]%% echo %mon% pause cls goto loopCan I change the highlighted # to make the words longer??? And if it's not too confusing, could yoyu maybe explain how it werks??If you change the length of the occurances in the array you'd have to make changes in two of the statements (see below). Use either trailing spaces or leading zeroes for padding if necessary. The example was SETUP with equal length occurances. For unequal length occurances you'd have to take a different approach and use a delimiter between each member of the array. If you change the number of members in the array, you would need to change the divisor for the num calculation. Code: [Select]@echo off set array=JanFebMarAprMayJunJulAugSepOctNovDec :loop set /a num=%random%/2730 if %num%==0 goto loop call set /a disp=[highlight]3[/highlight]*%num%-[highlight]3[/highlight] call set mon=%%array:~%disp%,[highlight]3[/highlight]%% echo %mon% pause cls goto loop How does it work? I could never do justice to the simple eloquence of this batch code. Turn echo on in the file. Watch and learn. A Windows script would be a better choice, but to each his own. 8-)I still don't quite get what #'s to change to change the random word! Like what would be changed if this digit was changed??: Code: [Select]@echo off set array=JanFebMarAprMayJunJulAugSepOctNovDec :loop set /a num=%random%/2730 if %num%==0 goto loop call set /a disp=[highlight]3[/highlight]*%num%-3 call set mon=%%array:~%disp%,3%% echo %mon% pause cls goto loopAnd what would be different if this digit was changed?: Code: [Select]@echo off set array=JanFebMarAprMayJunJulAugSepOctNovDec :loop set /a num=%random%/2730 if %num%==0 goto loop call set /a disp=3*%num%-[highlight]3[/highlight] call set mon=%%array:~%disp%,3%% echo %mon% pause cls goto loopThis one?: Code: [Select]@echo off set array=JanFebMarAprMayJunJulAugSepOctNovDec :loop set /a num=%random%/2730 if %num%==0 goto loop call set /a disp=3*%num%-3 call set mon=%%array:~%disp%,[highlight]3[/highlight]%% echo %mon% pause cls goto loopQuote Like what would be changed if this digit was changed??: The best answer would be for you to turn echo on, change each number and see what happens. Also check out the documentation for the SET statement which explains how to extract a substring from a string. Hint: the SET statement uses offset and length Good luck. 8-) I've noticed many of your posts concerning "DOS". Perhaps a trip to the library for a good reference would be helpful. Online, Allenware can be useful. Yes, i know it's not DOS!! it's command prompt! Thanks for all your help by the way! |
|