How to quickly generate random strings? Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
afe
Posts: 618
Joined: 06 Dec 2018, 04:36

How to quickly generate random strings?

17 May 2020, 12:06

How to quickly generate a random string of length 4 containing only numbers and lowercase letters? And allow up to 3 characters to be repeated.

The following code is too verbose, how can I improve it?

Code: Select all

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"

Loop, 3
   i .= s . ","

i := RTrim(i, ",")
Sort, i, Random D,
i := StrReplace(i, ",")
i := SubStr(i, 1, 4)
msgbox % i
Odlanir
Posts: 659
Joined: 20 Oct 2016, 08:20

Re: How to quickly generate random strings?

17 May 2020, 12:29

Code: Select all

str := "0123456789abcdefghijklmnopqrstuvwxyz"
s := StrSplit(str str str)
Loop, 4 {
   Random, rnd, 1, % s.MaxIndex() 
   i .= s.removeat(rnd) 
}
msgbox % i
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
User avatar
boiler
Posts: 6592
Joined: 21 Dec 2014, 02:44

Re: How to quickly generate random strings?

17 May 2020, 13:00

Another alternative:

Code: Select all

s := (s := "0123456789abcdefghijklmnopqrstuvwxyz") . s . s
loop, 4 {
	Random, r, 1, StrLen(s)
	i .= (c := SubStr(s, r, 1))
	StrReplace(s, c, "",, 1)
}
MsgBox, % i
User avatar
boiler
Posts: 6592
Joined: 21 Dec 2014, 02:44

Re: How to quickly generate random strings?

17 May 2020, 13:11

Code: Select all

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, ","), 1, 4)
MsgBox, % i
User avatar
Chunjee
Posts: 707
Joined: 18 Apr 2014, 19:05
GitHub: Chunjee

Re: How to quickly generate random strings?

18 May 2020, 14:38

afe wrote:
17 May 2020, 12:06
How to quickly generate a random string of length 4 containing only numbers and lowercase letters? And allow up to 3 characters to be repeated.
My take:

Code: Select all

A := new biga() ; requires https://www.npmjs.com/package/biga.ahk

characters := [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"]
randomString := A.join(A.sampleSize(characters, 4), "")
msgbox, % randomString
; => "is2a"
My attempt allowing for repeats but no more than 3 spec:

Code: Select all

A := new biga() ; requires https://www.npmjs.com/package/biga.ahk

characters := [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"]
randomArr := []
while (randomArr.Count() != 4) {
	randomChar := A.sample(characters)
	if (A.count(randomArr, randomChar) = 3) {
		continue
	}
	randomArr.push(randomChar)
}
randomString := A.join(randomArr, "")
msgbox, % randomString
; => "mqm8"
Last edited by Chunjee on 18 May 2020, 18:27, edited 1 time in total.
User avatar
boiler
Posts: 6592
Joined: 21 Dec 2014, 02:44

Re: How to quickly generate random strings?

18 May 2020, 15:12

Chunjee wrote:
18 May 2020, 14:38
My attempt following the 'no 3 characters repeat' spec:
You can have 3 repeated. You can't have all 4 be the same.
User avatar
SKAN
Posts: 853
Joined: 29 Sep 2013, 16:58

Re: How to quickly generate random strings?

18 May 2020, 15:25

boiler wrote:
17 May 2020, 13:11
Spoiler
Excellent! :thumbup:
User avatar
boiler
Posts: 6592
Joined: 21 Dec 2014, 02:44

Re: How to quickly generate random strings?

18 May 2020, 16:39

SKAN wrote:
18 May 2020, 15:25
Excellent! :thumbup:
Thanks! :)
malcev
Posts: 795
Joined: 12 Aug 2014, 12:37

Re: How to quickly generate random strings?  Topic is solved

18 May 2020, 17:11

I think that using autohotkey sort is not quick solution.
My variant:

Code: Select all

setbatchlines -1
a := a_tickcount
str := "0123456789abcdefghijklmnopqrstuvwxyz"
loop 1000000
{
   start:
   Random, rnd, 1, 36
   fin := SubStr(str, rnd, 1)
   Random, rnd, 1, 36
   fin .= SubStr(str, rnd, 1)
   Random, rnd, 1, 36
   fin .= SubStr(str, rnd, 1)
   Random, rnd, 1, 36
   fin .= SubStr(str, rnd, 1)
   if RegexMatch(fin, "^(.)\1+$")
      goto start
}
MsgBox, % a_tickcount - a
User avatar
Chunjee
Posts: 707
Joined: 18 Apr 2014, 19:05
GitHub: Chunjee

Re: How to quickly generate random strings?

18 May 2020, 17:28

boiler wrote:
18 May 2020, 15:12
You can have 3 repeated. You can't have all 4 be the same.
Ah funny enough my code will allow 3 repeats and not 4 by accident
User avatar
boiler
Posts: 6592
Joined: 21 Dec 2014, 02:44

Re: How to quickly generate random strings?

18 May 2020, 18:59

malcev wrote:
18 May 2020, 17:11
I think that using autohotkey sort is not quick solution.
True that it's not the fastest. Also not the stated goal in this case. He wanted it less verbose.
malcev
Posts: 795
Joined: 12 Aug 2014, 12:37

Re: How to quickly generate random strings?

19 May 2020, 05:47

The topic name is - "How to quickly generate random strings?"
Everyone has their own opinion about "quickly".
My code is nearly 7 times faster than Yours using sort command.
afe
Posts: 618
Joined: 06 Dec 2018, 04:36

Re: How to quickly generate random strings?

19 May 2020, 12:53

Code: Select all

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"

Loop, 3
   i .= s . ","

Sort, i, Random D,
i := SubStr(i, 1, 7)
i := StrReplace(i, ",")
msgbox % i
I simplified it a little bit.

Return to “Ask For Help”

Who is online

Users browsing this forum: dye5013, Google [Bot], hasantr, joefiesta, kunkel321, laulau, mikeyww, nanowiz and 41 guests