How to randomly select a character from a string

Get help with using AutoHotkey and its commands and hotkeys
SilasDeVis
Posts: 24
Joined: 18 Jan 2020, 08:42

How to randomly select a character from a string

14 Feb 2020, 08:31

me and a friend wanted to check on how to get a word exactly the same as a few random strings with some luck. We constantly challenge each other to program something, and now it is my turn, but i can't figure this out. I tried, but the only information i get from ToolTip are zero's and ones.

Code: Select all

InputBox, word,, Give a word with 5 characters. It can be low`, high letters and numbers.,HIDE,,,,,,,,
tries=0
string1 := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
string2 := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
string3 := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
string4 := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
string5 := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"	
Key=%string1%%string2%%string3%%string4%%string5%

Layer1:
Random, Var1, 1, string1
Random, Var2, 1, string2
Random, Var3, 1, string3
Random, Var4, 1, string4
Random, Var5, 1, string5
F1::
	MsgBox, %tries%
ToolTip, %Var1%%Var2%%Var3%%Var4%%Var5%
If (Key=word) {
Goto, Layer2
} Else {
tries++
Goto, Layer1
}
Layer2:
MsgBox, the word was %Woord%. Number of tries: %tries%
Anyone a idea on how Random will sending letters instead of numbers? Sorry for my bad english.
Last edited by SilasDeVis on 14 Feb 2020, 16:07, edited 6 times in total.
gregster
Posts: 4642
Joined: 30 Sep 2013, 06:48

Re: How to randomly select a character from a string?

14 Feb 2020, 08:59

SilasDeVis wrote:
14 Feb 2020, 08:31
Anyone a idea on how Random is sending letters instead of numbers?
While I would consider this alone to be a valid question, we don't want to support or promote (potentially) malicious uses of AHK code on this forum.

Hence, I would consider the current name of this topic and the general approach you are using at least borderline problematic.
How about you change the topic name (for example to "How to randomly select a character from a string?") and edit your original post so that it focuses on the question about how Random can be used to select a character from a string!?

I would still strongly recommend to not use brute force (or any other method) to "crack" passwords, at least not on other systems, the internet or any files that don't belong to you.

Edit: Thank you. That's better...
SilasDeVis
Posts: 24
Joined: 18 Jan 2020, 08:42

Re: How to randomly select a character from a string

14 Feb 2020, 12:39

Thank you for your comment. I changed it, and i hope this now will meet your meanings to. Me and a friend are contantly challenging each other to program something, and he gave me a exercise to program a ahk file that could "guess" or calculate his given word. not just display it.(I did that last time and when i showed him my code i "lost", so i got minus one point. We aren't using any of these program or files to crack other computers, or hack some websites or something. We just wanted to learn the code as fast as possible to go on with higher maths and in the end AI, and this seemed to be the best way.
User avatar
Yakshongas
Posts: 514
Joined: 21 Jan 2020, 08:41
Facebook: Yakshongas

Re: How to randomly select a character from a string

14 Feb 2020, 16:43

I also tried but didn't find a way, but i made one that does the same thng but with numbers.

Code: Select all

SetBatchLines -1
SetWorkingDir %A_ScriptDir%

Inputbox, output, Some text, 5 Random numbers, HIDE, 289, 130
If (ErrorLevel=1)
{
	Return
}

tries=0
phase1:
Random, number1, 0, 9
Random, number2, 0, 9
Random, number3, 0, 9
Random, number4, 0, 9
Random, number5, 0, 9

ToolTip, ---%number1%%number2%%number3%%number4%%number5%---

key = %number1%%number2%%number3%%number4%%number5%


if (output=key)
{
	MsgBox, Number is, %key%, number of tries: %tries%
	Reload
}
Else
{
tries++
goto, phase1
}
F1::
MsgBox, Number of tries: %tries%
goto, phase1
Please mark your topics as solved if you don't need any further help. ✅
User avatar
Xtra
Posts: 1790
Joined: 02 Oct 2015, 12:15

Re: How to randomly select a character from a string

14 Feb 2020, 18:57

Example:

Code: Select all

#NoEnv

InputBox, word,, Give a word with upto 5 characters. It can be Lowercase, Uppercase and Numbers.
if (ErrorLevel)
    return
RandomChars(word, match, tries)
MsgBox, 4160, Word input = %word%, The match was %match%.`nNumber of tries: %tries%
ExitApp

RandomChars(word, ByRef match, ByRef cnt)
{
    static str := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
    static sLen := 62
    SetBatchLines, -1
    wLen := StrLen(word)
    Loop
    {
        match := "" , cnt := A_Index
        Loop % wLen {
            Random, n, 1, sLen
            ToolTip % match .= SubStr(str, n, 1)
        }
    }until (word == match)
    ToolTip
}
Pretty much useless for anything serious, its just for fun.
SilasDeVis
Posts: 24
Joined: 18 Jan 2020, 08:42

Re: How to randomly select a character from a string

15 Feb 2020, 04:47

I think it worked. It isn't fast, but it'll get there. Your code looks very complicated for me, and autohotkey doesn't explain the command "RandomChar". Can you help me out with that? Already thanks a lot!
User avatar
Xtra
Posts: 1790
Joined: 02 Oct 2015, 12:15

Re: How to randomly select a character from a string

15 Feb 2020, 14:37

RandomChars() is a created function not a built in AHK function.
If it was a built in function you would only see the function call in the script.RandomChars(word, match, tries)

Click any of the commands in the code box to bring up the help file for it.
AHKStudent
Posts: 882
Joined: 05 May 2018, 12:23

Re: How to randomly select a character from a string

15 Feb 2020, 23:38

I don't understand what you need, but to select a random letter from a string try this

Code: Select all

string1 := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
random, rs, 1, % strlen(string1)
randomstr := strsplit(string1) 
msgbox, % randomstr[rs]
User avatar
Chunjee
Posts: 602
Joined: 18 Apr 2014, 19:05
GitHub: Chunjee

Re: How to randomly select a character from a string

17 Feb 2020, 15:28

I did it in my style but it hasn't guessed "hello" yet after 10 mins.

Code: Select all

; REQUIRES https://www.npmjs.com/package/biga.ahk
A := new biga()
tries := 0
charOptions := A.split("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890", "")

InputBox, word,, Supply a word with 1-5 characters. It can be low`, high letters and numbers.

while (true) {
    ; try to guess the user's input by random sampling
    tries++
    computerGuess := ""
    loop, % A.size(word) {
        computerGuess .= A.sample(charOptions)
    }
    ToolTip, %computerGuess%
    if (word == computerGuess) {
        msgbox, % "GUESSED '" computerGuess "' CORRECTLY AFTER ONLY " tries " ATTEMPTS!"
        break
    }
}
exitapp
Could also make use of https://biga-ahk.github.io/biga.ahk/#/?id=samplesize if you wanted to guess without any repeat characters
User avatar
Chunjee
Posts: 602
Joined: 18 Apr 2014, 19:05
GitHub: Chunjee

Re: How to randomly select a character from a string

17 Feb 2020, 15:36

Guessing a 3 character word took 34219 attempts in this case
User avatar
Chunjee
Posts: 602
Joined: 18 Apr 2014, 19:05
GitHub: Chunjee

Re: How to randomly select a character from a string

03 Mar 2020, 09:58

Code: Select all

; split user input up and add to charOptions so that user can never enter a character that is not in the options list
wordArry := A.split(word, "")
charOptions := A.uniq(A.concat(charOptions, wordArry))

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], boiler, Google [Bot], Jstenoien, karkas, Rafaews, swagfag, Xeo786 and 52 guests