| 1. |
Solve : random?? |
|
Answer» hi is there a peice of code that will pick a variable from a list out at randomYes...by picking out from array but random will only follow algorithm UNLESS you seed it. Random will always pick say 3,4,7,5,2 in that order if executed again and again after closing it unless you add a seed to influence the algorithms output in which the output is less likely to be guessed in the order at which it will spit out the outcome. Example for choosing a random # from 0 to 10:Really? I always have to use goto loop so I can get a number...but depending on the number, can take a fairly long time. Code: [Select]set /a return=%RANDOM%%10 that will only generate a rand number. imagine a list of numbers eg 10,20,30,40,50. you will need to get a random item out of this list, not by simply using the above code. Quote from: gh0std0g74 on June 16, 2009, 06:43:00 PM Code: [Select]set /a return=%RANDOM%%10Well, most more advanced needs require more lines in a script. echo off :loop set /a rnd=%random%%50 if %rnd% neq 50 if %rnd% neq 40 if %rnd% neq 30 if %rnd% neq 20 if %rnd% 10 goto loop echo The random number is either 10, 20, 30, 40 or 50. echo Your random number is %rnd%. pause > nul Quote from: Helpmeh on June 17, 2009, 04:05:23 PM Well, most more advanced needs require more lines in a script.not really. if %RANDOM% doesn't hit a number in the list forever, are you going to wait forever?? use the number of items on the list with %RANDOM% instead. Quote from: gh0std0g74 on June 17, 2009, 06:08:30 PM not really. if %RANDOM% doesn't hit a number in the list forever, are you going to wait forever?? :-Xuse the number of items on the list with %RANDOM% instead.I personally have never used the %random%%10 style, but the 10, 20, 30, 40, 50 numbers was just an example. For your example getting a random choice of a certain list, this would work. It would also work as a much quicker version than my previous code. echo off set /a rnd=%random%%5 set /a rnd*=10 echo %rnd% pause>nul Quote from: Helpmeh on June 17, 2009, 06:48:32 PM I personally have never used the %random%%10 style, but the 10, 20, 30, 40, 50 numbers was just an example.and if the list of numbers is 1,2,3,4,11,13,16 for example, your code will not work isn't it? what i am saying , 1,2,3,4,11,13,16 has 7 items. generate a random number from 1 to 7, (not the max value of the item ie 16). If the random number is 5, then use a loop or something, go through the list to position 5 and get the number "11"Yeah, I was just taking advantage of the consecutive numbers. echo off set /a rnd=%random%%7 if %rnd%==5 set rnd=11 if %rnd%==6 set rnd=13 if %rnd%==7 set rnd=16 echo %rnd% pause > nul would do the trick for your example. Quote from: Helpmeh on June 17, 2009, 07:38:03 PM would do the trick for your example.not enough. try not to hard code those numbers. Quote from: gh0std0g74 on June 17, 2009, 07:54:59 PM not enough. try not to hard code those numbers.I'm SLIGHTLY confused now...you want them set as variables? echo off set var1=1 set var2=99 set var3=1234 set var4=12 set var5=6 set /a rnd=%random%%5 set varout=%var%%rnd% echo %varout% pause>nul Hope that works!I can't believe anybody would use a loop to generate random numbers over and over... ever. Even the case, where there is one index that is unused (IE, 5) can be RESOLVED without the need to resort to a looping mechanism. For example: Code: [Select]1,2,3,4,11,13,16 the easiest way, as ghostdog said, is essentially to index the values. Code: [Select]Sub Test() Dim arrayTest as Variant Dim resultrnd as Long ArrayTest = Array(1,2,3,4,11,13,16) resultrnd = ArrayTest(Rnd*Ubound(arraytest)+Lbound(arraytest)) End Sub if you had a disjoint array somehow, simply inspect the returned random number; add a number equal to the amount of "nil" space that it is above. |
|