Page 1 of 1

Random string script with random length

Posted: 01 Jun 2023, 19:41
by ARkaine
Help make random string script with random length (in probably specified range, something like from 8 to 24 characters),
something like based on this thread:
https://www.autohotkey.com/boards/viewtopic.php?t=76052

ATM I have such script:

^SC039:: ; Ctrl+Space as
s := (s := "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z, ") "," s "," s
Sort, s, Random D,
i := SubStr(StrReplace(s, ","), 2, 23)
Send, % i

Re: Random string script with random length  Topic is solved

Posted: 01 Jun 2023, 19:45
by mikeyww
What does your script actually do?
What should it do instead?
What is your example?

Code: Select all

#Requires AutoHotkey v1.1.33

^Space::
str := ""
Random n, 8, 24
Loop % n {
 Random c, 33, 126
 str .= Chr(c)
}
SendInput % "{Text}" str
Return

Code: Select all

#Requires AutoHotkey v1.1.33

^Space::
s := "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z"
Random n, 8, 24
Sort s, Random D,
SendInput % SubStr(StrReplace(s, ","), 1, n)
Return

Re: Random string script with random length

Posted: 02 Jun 2023, 09:23
by ARkaine
mikeyww,

I often improvise in DAW (digital audio workstation),
and sometimes I want to quickly save new project,
so, I prefer to give random name to it
(anyway it will be in list of recent projects);
so, I searched for way to make (enter) random name (string):
let's say after pressing Ctrl+Space
which at first sends Ctrl+S or Ctrl+Alt+Shift+S
and then enters random name (string).

Thanks, your answer helped me. :)

Re: Random string script with random length

Posted: 02 Jun 2023, 09:36
by wetware05
hi, ARKaine and mikeyww

I think mikeyww's first script fails because it adds characters that are not accepted when creating a file/directory name in Windows directives, like \ or @. The second is better.

Re: Random string script with random length

Posted: 02 Jun 2023, 16:29
by mikeyww
Yes; the need was originally unclear.

A simpler approach may be using a simple numeric file suffix. One can check to see whether the file already exists, and then increment the numeric suffix until the file is not found. This may be more reliable and easier than selecting a random number.

Re: Random string script with random length

Posted: 03 Jun 2023, 11:34
by Chunjee
For fun:

Code: Select all

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

inputArray := StrSplit("0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z", ",")
msgbox, % A.join(A.sampleSize(inputArray, A.random(8, 24)), "")
; => "vcap6lwi24sy59gnoxefm1u7"
Because https://biga-ahk.github.io/biga.ahk/#/?id=samplesize will not sample the same element twice, this will always produce strings with no repeat characters