Random

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Totte
Posts: 5
Joined: 09 Jun 2023, 03:47

Random

Post by Totte » 09 Jun 2023, 04:08

Hello hope you can help me here!
i´m new to this, hope i can get help anyway? sorry for my english!
i want this to work, but maybe its all wrong.
Ex: i want it to write random, Hello or Hi or Hey! How are you or How is it with you? What are you doing or Tell me what you are doing now?
Is it possible to do so?

Code: Select all

F1::
string = {Hello | Hi |Hey }! {How is it | How are you | How is it with you}? {What are you doing | Tell me what you are doing now}?

clipboard:=spintax(string)
;send, ^v

spintax(string) {
		Pos := 1
		while (Pos := RegExMatch(string, "{([^}]*)}" M, Pos + StrLen (R)))
				string := StrReplace(string, M, R := getRandomElement(StrSplit(M1, "|")))
				
		return string
}

getRandomElement(Arr) {
		Random rand, 1, % Arr.Count()
		return Arr.RemoveAt(rand)
}

Best reguard Tony


[Mod edit: Added [code][/code] tags. Please use them yourself when posting code.]

User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: Random

Post by mikeyww » 09 Jun 2023, 05:45

Welcome to this AutoHotkey forum!

Code: Select all

#Requires AutoHotkey v1.1.33
msg :=  [["!", ["Hello", "Hi", "Hey"]                                   ]
       , ["?", ["How is it", "How are you", "How is it with you"]       ]
       , ["?", ["What are you doing", "Tell me, what you are doing now"]]]

F3::
For n, sentence in msg {
 Random phraseNum, 1, sentence[2].Length()
 SendInput % "{Text}" (n > 1 ? " " : "") sentence[2][phraseNum] sentence[1]
}
Return

Totte
Posts: 5
Joined: 09 Jun 2023, 03:47

Re: Random

Post by Totte » 09 Jun 2023, 09:12

Thank you very mutch for your help, it work :)

Best reguard Tony

User avatar
Chunjee
Posts: 1400
Joined: 18 Apr 2014, 19:05
Contact:

Re: Random

Post by Chunjee » 09 Jun 2023, 09:24

For fun:

Code: Select all

A := new biga() ; requires https://github.com/biga-ahk/biga.ahk

greetingsArr := ["Hello!", "Hi!", "Hey!", "Hello", "How is it?", "What are you doing?"]

F1::
randomGreet := A.sample(greetingsArr)
sendInput, % randomGreet
return

https://biga-ahk.github.io/biga.ahk/#/?id=sample

Post Reply

Return to “Ask for Help (v1)”