Random string script with random length Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
ARkaine
Posts: 20
Joined: 30 Dec 2019, 07:58

Random string script with random length

Post by ARkaine » 01 Jun 2023, 19:41

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

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

Re: Random string script with random length  Topic is solved

Post by mikeyww » 01 Jun 2023, 19:45

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

ARkaine
Posts: 20
Joined: 30 Dec 2019, 07:58

Re: Random string script with random length

Post by ARkaine » 02 Jun 2023, 09:23

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

wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: Random string script with random length

Post by wetware05 » 02 Jun 2023, 09:36

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.

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

Re: Random string script with random length

Post by mikeyww » 02 Jun 2023, 16:29

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.

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

Re: Random string script with random length

Post by Chunjee » 03 Jun 2023, 11:34

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

Post Reply

Return to “Ask for Help (v1)”