Is there a way to prevent a random letter appearing more than once? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
burken
Posts: 5
Joined: 10 May 2021, 20:21

Is there a way to prevent a random letter appearing more than once?

Post by burken » 01 Jun 2021, 05:20

Hello.

With the current code I made, I get five random letters between A-F but they can appear more than once. I don't want that.
When I press F2 I would like it to send," D E C B A " or " F A D C E " etc... and not, for example, " A C A D E " with "A" appearing twice. (A, B, C, .. F represent different words)

Is there a way to send 5 random letters between A-F without a single letter appearing more than once?

Code: Select all

F2::
arr1 :=["A ","B ","C ","D ","E ","F "]
arr2 :=["A ","B ","C ","D ","E ","F "]
arr3 :=["A ","B ","C ","D ","E ","F "]
arr4 :=["A ","B ","C ","D ","E ","F "]
arr5 :=["A ","B ","C ","D ","E ","F "]
Random, Rand1, 1, arr1.Count(6)
Random, Rand2, 1, arr2.Count(6)
Random, Rand3, 1, arr3.Count(6)
Random, Rand4, 1, arr4.Count(6)
Random, Rand5, 1, arr5.Count(6)

Send % arr1[rand1] "" arr2[rand2] arr3[rand3] arr4[rand4] arr5[rand5] "`n"
return
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Is there a way to prevent a random letter appearing more than once?  Topic is solved

Post by mikeyww » 01 Jun 2021, 06:12

Code: Select all

F2::
letter := ["A","B","C","D","E","F"], str := ""
While letter.Count() {
 Random, rand, 1, letter.Count()
 str .= " " letter[rand], letter.RemoveAt(rand)
}
Send % (str := Trim(str)) "`n"
Return
burken
Posts: 5
Joined: 10 May 2021, 20:21

Re: Is there a way to prevent a random letter appearing more than once?

Post by burken » 01 Jun 2021, 06:15

Exactly what I was trying to do, thank you!
Post Reply

Return to “Ask for Help (v1)”