0
votes

The script need to output a file with the names in random:

Gui, Add, Edit, x25 y25 h150 w200 vSN, Enter names here
Gui, Add, Edit, x25 y185 h25 w200 vRT, Project Name
Gui, Add, Button, x25 y225 h25 w200, Generate
Gui, Show, x375 y200 h270 w250, Area Assignment by PS Cebu Team
FormatTime, TimeString,, MM d, yyyy
return
ButtonGenerate:
Gui, Submit, NoHide
{ 
  count := 1
  StringSplit, lines, SN, `n
  Loop, %lines0%
  {
    content := lines%a_index%
    FileAppend, Area %count% --- %content% `n, %RT% - %TimeString%.txt
    count++
  }
  return
  guiclose:
  exitapp
}

names on the output should be randomized.

2

2 Answers

2
votes

Use:

Sort, SN, Random

This will sort your string randomly... After that, that is when you should Split it.

Sorting an array randomly is not as easy because it has keys and value pairs.
So we try to randomize it before we convert it to array.

Sort also defaults to delimit new lines. But if you want to use a custom delimiter use the D option. Like:

# this will sort a string delimited by a comma
Sort, SN, Random D,

Here is an untested code (because I don't use AHK) but it should work:

Gui, Add, Edit, x25 y25 h150 w200 vSN, Enter names here
Gui, Add, Edit, x25 y185 h25 w200 vRT, Project Name
Gui, Add, Button, x25 y225 h25 w200, Generate
Gui, Show, x375 y200 h270 w250, Area Assignment by PS Cebu Team
FormatTime, TimeString,, MM d, yyyy
return
ButtonGenerate:
Gui, Submit, NoHide
{ 
  count := 1
  # randomly sort the string delimited by `n first
  Sort, SN, Random
  # then convert it to the array
  StringSplit, lines, SN, `n
  Loop, %lines0%
  {
    content := lines%a_index%
    FileAppend, Area %count% --- %content% `n, %RT% - %TimeString%.txt
    count++
  }
  return
  guiclose:
  exitapp
}
2
votes

If you are an object oriented type of programmer, you could also go with actual arrays in AHK_L:

SN = James`nJohn`nRobert`nMichael

nameArr := StrSplit(SN, "`n")
ArrayShuffle(nameArr)
for i, name in nameArr
{
    FileAppend, Random name: %name%, somewhere.txt
}

; Fisher-Yates shuffle
ArrayShuffle(arr) {
    i := arr.MaxIndex()
    while(i > 1) {
        Random, rnd, 1, % i
        tmp := arr[rnd]
        arr[rnd] := arr[i]
        arr[i] := tmp
        i--
    }
    return arr
}

This way, you have control over the shuffling algorithm, I used an implementation of Fisher–Yates shuffle. You can also take this a step further and utilize Arrays.ahk, optionally adding the shuffling function to it.

P.S:
Maybe, you should check the contents of the Project name edit for validity, since you're using them directly as a file name. If a user accidentally types in an illegal character, your program will crash.