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.

I have never seeded a random in a batch to give an example but am guessing that it follows the same rules as C, C++, or Perl with Time used.if you can download gawk for windows (see my sig)
Code: [Select]
C:\test>echo 1 2 3 4 5 10 8 5|gawk "BEGIN{srand()}{R=int(1+rand()*NF); print $(R)}"
8

C:\test>echo 1 2 3 4 5 10 8 5|gawk "BEGIN{srand()}{R=int(1+rand()*NF); print $(R)}"
4

C:\test>echo 1 2 3 4 5 10 8 5|gawk "BEGIN{srand()}{R=int(1+rand()*NF); print $(R)}"
4

C:\test>echo 1 2 3 4 5 10 8 5|gawk "BEGIN{srand()}{R=int(1+rand()*NF); print $(R)}"
3

C:\test>echo 1 2 3 4 5 10 8 5|gawk "BEGIN{srand()}{R=int(1+rand()*NF); print $(R)}"
5

C:\test>


No code is needed

Run CMD.EXE and type SET /?
At the end of several pages is a SMALL section on dynamic environmental variables that are sort of secret - if you do not know them you cannot see them.

%RANDOM% is one of them
ECHO %RANDOM% will display a (semi)random number that changes every time.

If you want a truly random sequence you will need to seed it.

Regards
Alan
Example for choosing a random # from 0 to 10:

Code: [Select]set /a return=%RANDOM%%10 Quote from: macdad- on June 16, 2009, 04:27:48 PM

Example for choosing a random # from 0 to 10:

Code: [Select]set /a return=%RANDOM%%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%%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.
Well, 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.

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
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.

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
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.



Discussion

No Comment Found