如何选择随机数组,然后选择THAT数组中的随机索引?

时间:2017-10-12 14:17:59

标签: arrays random return arguments autohotkey

;Sayings
friendly := ["hello there!", "Howdy!", "Greetings friend!", "hi there"]
helpful := ["x next to y and z", "this is helpful", "The sky is blue"]
other := ["where is my toaster", "do it", "just"]

;Categories
welcome := [[friendly][helpful][other]]

Numpad0::
    intotext(randomfromarray("friendly"),50,35,15)
    return

Numpad1::
    intotext(randomfromarray("helpful"),50,35,15)
    return

;Numpad2::
    ;Select a random saying from the welcome category.

intotext(whatisit,thekeydelay,introwait,outrowait)
{
    SetKeyDelay, thekeydelay
    Send, t
    Sleep, %introwait%  
    Send, %whatisit% 
    Sleep, %outrowait%
    Send, {enter}
}

randomfromarray(uniquepickof){
    return %uniquepickof%[random(1, %uniquepickof%.maxindex() )]
}

random(x, y){
   Random, var, %x%, %y%
   return var
}
  1. 有人可以帮我搞清楚(对于Numpad2::)如何从Welcome类别中选择一个随机的Sayings数组,然后选择那个(说)数组的随机索引?
  2. 我试过抛出一些想法,但它们都只是因为我没有正确地返回(选择随机数组)[index]值而得到的结果。

1 个答案:

答案 0 :(得分:0)

所以这就是我的工作:

;Sayings
friendly := ["hello there!", "Howdy!", "Greetings friend!", "hi there"]
helpful := ["x next to y and z", "this is helpful", "The sky is blue"]
other := ["where is my toaster", "do it", "just"]

;Categories
welcome := [friendly, helpful, other]

Random, randNumOne, 1, welcome.MaxIndex()

MsgBox % "Your random number is for array is: " . randNumOne

randArray := welcome[randNumOne]

Random, randNumTwo, 1, randArray.MaxIndex()

MsgBox %  "Your random Saying is " . randArray[randNumTwo]

为简化起见,我删除了自定义函数,它实际上只是内置Random函数的包装器。我离开了MsgBox,以便您可以将输出验证为随机。

这里是你的整个脚本,新的随机选择功能分配给Numpad2按键:

;Sayings
friendly := ["hello there!", "Howdy!", "Greetings friend!", "hi there"]
helpful := ["x next to y and z", "this is helpful", "The sky is blue"]
other := ["where is my toaster", "do it", "just"]

;Categories
welcome := [friendly, helpful, other]

Numpad0::
    intotext(randomfromarray("friendly"),50,35,15)
    return

Numpad1::
    intotext(randomfromarray("helpful"),50,35,15)
    return

Numpad2::
    Random, randNumOne, 1, welcome.MaxIndex()

    MsgBox % "Your random number is for array is: " . randNumOne

    randArray := welcome[randNumOne]

    Random, randNumTwo, 1, randArray.MaxIndex()

    MsgBox %  "Your random Saying is " . randArray[randNumTwo]
    return

intotext(whatisit,thekeydelay,introwait,outrowait)
{
    SetKeyDelay, thekeydelayt
    Send, t
    Sleep, %introwait%  
    Send, %whatisit% 
    Sleep, %outrowait%
    Send, {enter}
}

randomfromarray(array)
{
    Random, rand, 1, array.MaxIndex()

    return array[rand]
}