Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

[Function]Random string generator


  • Please log in to reply
4 replies to this topic
Sjc1000
  • Members
  • 572 posts
  • Last active: Mar 11 2017 11:41 AM
  • Joined: 06 Feb 2012

I recently made a function to generate a random string, and i know this has been done before.

This function will enable you to have "options" for example.

SC_RandomString("M10 D3 U L ")  " M10 " is the maximum numbers there will be and "N3 U L" is the options. D = Number ( digit ) , U = Upper case and L = Lower case. If you put a number next to it, there will be ( in this example ) a minimum of 3 numbers, and the other 7 digits will be completely random while still following the options ( Number , Upper case, and lower case).

MsgBox, % SC_RandomString(" M10 U4 L ")  this will generate a random string without numbers because i did not put N in the options, so there will be 4 upper case and the rest will be random between upper case and lower case and no numbers

Alright i think that is enough of the examples.

 

 

;------------------------------------ SC_RandomString ----------------------------------
; This will generate a random string according to options
; U = Upper case
; L = Lower case
; D = Digit ( number )
; M = Maximum numbers
;
; Specify one of these letters and then ( optional ) have a number after it
; That will be the Minimum number of that type of character
; The rest of the characters will be random ( while still fitting the options )
;
; This will return 1 if the Max ( M10 ) is samller than all the other options that add up
;
; Example
;-----------
 MsgBox,% SC_RandomString(" M10 U L2 D5")   ; This will have a Minimum of 2 lower case and 5 Digits, everything else will be a random between U L and D
;---------------------------------------------------------------------------------------------
SC_RandomString( Options )
{ Div := 0, Total := 0
If not RegExMatch( Options, "(M|m)(\d+)", _)
Return 1
If RegExMatch( Options, "(U|u)(\d?)", __)
Div++, U := 1
If RegExMatch( Options, "(L|l)(\d?)", ___)
Div++, L := 1
If RegExMatch( Options, "(D|d)(\d?)", ____)
Div++, D := 1
Max := _2, Un := __2, Ln := ___2, Dn := ____2, Un := Un > 0 ? Un : 1, Ln := Ln > 0 ? Ln : 1, Dn := Dn > 0 ? Dn : 1, Total := ( Un + LN + Dn ) , Un := U < 0 ? 0 : Un, Ln := L < 0 ? 0 : Ln, Dn := D < 0 ? 0 : Dn
If ( Un + LN + Dn ) > Max 
Return 2
SC_Loop:
Loop, %Total%
{ Random, Ran, 65, 90
Out .= Un > 0 ? ( Chr(Ran) , Un-- ) : "" 
If StrLen( Out ) = Max
Goto, End
Random, Ran, 97, 122
Out .= Ln > 0 ? ( Chr(Ran), Ln-- ) : ""
If StrLen( Out ) = Max
Goto, End
Random, Ran, 0,9
Out .= Dn > 0 ? ( Ran, Dn-- ) : ""
If StrLen( Out ) = Max
Goto, End
}
If StrLen( Out ) < Max
{ Un+= U > 0 ? 1 : 0,Ln+= L > 0 ? 1 : 0,Dn+= D > 0 ? 1 : 0
Goto, SC_Loop
}
End:
Loop, Parse, Out,
{ gOut .= A_LoopField ","
Sort, gOut, Random D`,
}
Loop, Parse, gOut, `,
{ If A_LoopField = %Last%
Return SC_RandomString( Options ) 
Last := A_LoopField
}
StringReplace, Out, gOut, `,,,All
Return Out
}
;--------------------------------- End of function --------------------------------

 


Sjc1000 - Insert inspirational quote here!

PLEASE find me on the IRC if you have questions. I'm never on the forum anymore.

 


MasterFocus
  • Moderators
  • 4323 posts
  • Last active: Jan 28 2016 01:38 AM
  • Joined: 08 Apr 2009

Here's a quick list of related posts, in case anyone is interested:
 
• [Request] How can I use ahk to create a random password? - http://www.autohotke...php?f=1&t=92440
   (this is the request that sjc1000 has mentioned)
• [Function] RandomVar() - http://www.autohotke...php?f=2&t=47104
• [Request] How to generate a random string ... - http://www.autohotke...php?f=1&t=18344
• [Function] password generator - http://www.autohotke....php?f=2&t=5572
• [Script] Random character generator - http://www.autohotke...php?f=1&t=66823
• [Script] Password Generator 8 ... - http://www.autohotke...php?f=2&t=47640


-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Antonio França -- git.io -- github.com -- ahk4.net -- sites.google.com -- ahkscript.org

Member of the AHK community since 08/Apr/2009. Moderator since mid-2012.


just me
  • Members
  • 1496 posts
  • Last active: Nov 03 2015 04:32 PM
  • Joined: 28 May 2011
Why did you post this in the Custom section? It's pure AHK_Basic syntax.

Prefer ahkscript.org for the time being.


Sjc1000
  • Members
  • 572 posts
  • Last active: Mar 11 2017 11:41 AM
  • Joined: 06 Feb 2012
Sorry, im not sure what is AHK_L Syntax and what is not, i use AHK_L so i just post in the Custom. A mod can move it if he/she really feels like it is not in the right spot.

Sjc1000 - Insert inspirational quote here!

PLEASE find me on the IRC if you have questions. I'm never on the forum anymore.

 


Sjc1000
  • Members
  • 572 posts
  • Last active: Mar 11 2017 11:41 AM
  • Joined: 06 Feb 2012

Updated the code to be smaller, you will notice it is about 30 ish MS slower ( because of the RegEx ).


Sjc1000 - Insert inspirational quote here!

PLEASE find me on the IRC if you have questions. I'm never on the forum anymore.