Page 1 of 1

How to randomly select a character from a string

Posted: 14 Feb 2020, 08:31
by SilasDeVis
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.

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

Posted: 14 Feb 2020, 08:59
by gregster
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...

Re: How to randomly select a character from a string

Posted: 14 Feb 2020, 12:39
by SilasDeVis
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.

Re: How to randomly select a character from a string

Posted: 14 Feb 2020, 16:43
by Yakshongas
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

Re: How to randomly select a character from a string

Posted: 14 Feb 2020, 18:57
by Xtra
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.

Re: How to randomly select a character from a string

Posted: 15 Feb 2020, 04:47
by SilasDeVis
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!

Re: How to randomly select a character from a string

Posted: 15 Feb 2020, 14:37
by Xtra
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.

Re: How to randomly select a character from a string

Posted: 15 Feb 2020, 23:38
by AHKStudent
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]

Re: How to randomly select a character from a string

Posted: 17 Feb 2020, 15:28
by Chunjee
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

Re: How to randomly select a character from a string

Posted: 17 Feb 2020, 15:36
by Chunjee
Guessing a 3 character word took 34219 attempts in this case

Re: How to randomly select a character from a string

Posted: 03 Mar 2020, 09:58
by Chunjee

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))