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"