Here's a slightly more thought out version:
Code:
;var=aa,bbb,cccc,dd,eee,ffff,gg,hhh,iiii,jj,kkk,llll
var=
(
aa
bbbbb
dddd
eeee
tadd
egg
mad
canibal
ddd
rteter
egglo
hhhhhhr
rtrrtrtr
)
;!j::MsgBox % rItems(4,var,"`,")
!j::MsgBox % rItems(4,var,"`n","`r")
/*
no - number of items to retrieve
input - list variable with the items to be retrieved
delim - item delimiter
omit - omit character (optional)
*/
rItems(no,input,delim,omit="") {
VarSetCapacity(res,0) ; sets res to blank to check parameters
StringReplace, max, input, % delim, , UseErrorLevel ; check no. of items by delimiter
if (ErrorLevel < no)
return res,VarSetCapacity(max,0) ; if 'no' exceeds ErrorLevel returns nothing, out of bounds
Sort, input, Random D%delim% ; sorts input randomly by delimiter
Loop, Parse, input, % delim, % omit
{
if (A_Index > no)
break ; breaks loop once A_Index exceeds no. of items
else
res .= A_LoopField "`n"
}
return res,VarSetCapacity(max,0)
}