Random value from array (time related)

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Yoshimu
Posts: 38
Joined: 14 Nov 2014, 09:02

Random value from array (time related)

06 Dec 2015, 12:04

Hello,
Like i said in topic, i wanna choose random position from array. To simplify Array will content 60 positions -> and changing value will be secounds (a_sec).
First of all, i dont wanna assign value on the beginning of the script. (because the value on the whole script will be de same). Like this:

Code: Select all

Array1 := Array("Var1", "Var2", "Var3", "Var4", "Var60")
Arr1 := Array1[ "a_sec" ]
Send, %Arr1%
Sleep, 5000
Send, %Arr1%
And now, i want would like to do the same, but not Assigning Arr1 but use Array1[ "a_sec"] in the "Send" function.
Is it possible to do that?
I hope you know what i mean.

Thanks for any help.
Peabianjay
Posts: 52
Joined: 07 Nov 2015, 22:50

Re: Random value from array (time related)

06 Dec 2015, 12:18

Not entirely sure I know what you're asking, but maybe something like this?

Code: Select all

Array1 := Array("Alpha", "Beta", "Gamma", "Delta", "Omega")

loop, 5
{
    msgbox, % Array1[random(1, array1.maxindex() )]
}
exitapp

random( x, y )
{
   Random, var, %x%, %y%
   return var
}
Yoshimu
Posts: 38
Joined: 14 Nov 2014, 09:02

Re: Random value from array (time related)

06 Dec 2015, 12:34

hmm, thats help, but what is : random (x,y) {....}
What is the Name of it in AHK?
Peabianjay
Posts: 52
Joined: 07 Nov 2015, 22:50

Re: Random value from array (time related)

06 Dec 2015, 13:05

Well, you said you were looking for a "random position from the array".

The AHK command is https://autohotkey.com/docs/commands/Random.htm

I wrote a small function called "random( x, y)" that uses that command to generate a random number.

This: array1.maxindex() tells me how many values in the array (5, in this example).
So this:, random(1, array1.maxindex() ) generates a random number from 1 to 5.
Then this: Array1[ random(1, array1.maxindex() ) ] is any of Array1[1], Array[2], ... Array[5]

And, finally, msgbox, % Array1[ random(1, array1.maxindex() ) ] create a message box and use the contents of the variables.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: haomingchen1998, mikeyww, mmflume, roysubs, scriptor2016, ShatterCoder and 106 guests