CFLib.org – Common Function Library Project

randStr(strLen, charset)

Last updated April 2, 2007
Download UDF

author

Bobby Hartsfield                                  Bobby Hartsfield

Version: 2 | Requires: ColdFusion 5 | Library: StrLib

 
Rated 2 time(s). Average Rating: 5.0

Description:
randStr returns a randomly generated string using lowercase letters, and/or numbers and possible uppercase letters. The length of the string can be set specifically or the function can choose a random length within a specified range. eg: "8,16" will generate a string 8 to 16 characters long: "5" would only generate strings with 5 characters. The characters used are based on your own preference. You may build a 'charset' using any of the three words "alpha, numeric, and upper". Alpha = a-z Upper = A-Z Numeric = 0-9

Return Values:
Returns a string.

Example:

<cfoutput>
#randStr("8,16", "alphanumericupper")#
</cfoutput>

Parameters:

Name Description Required
strLen Either a number of a list of numbers representing the range in size of the returned string. Yes
charset A string describing the type of random string. Can contain: alpha, numeric, and upper. Yes

Full UDF Source:

<cfscript>
/**
* Returns a randomly generated string using the specified character sets and length(s)
*
* @param strLen      Either a number of a list of numbers representing the range in size of the returned string. (Required)
* @param charset      A string describing the type of random string. Can contain: alpha, numeric, and upper. (Required)
* @return Returns a string.
* @author Bobby Hartsfield (bobby@acoderslife.com)
* @version 2, April 2, 2007
*/

function randStr(strLen, charSet) {
    var usableChars = "";
    var charSets = arrayNew(1);
    var tmpStr = "";    
    var newStr = "";
    var i = 0;
    var thisCharPos = 0;
    var thisChar = "";
    
    charSets[1] = '48,57';    // 0-9
    charSets[2] = '65,90';    // A-Z
    charSets[3] = '97,122';    // a=z
    
    if (findnocase('alpha', charSet)) { usableChars = listappend(usableChars, 3); }
    
    if (findnocase('numeric', charSet)) { usableChars = listappend(usableChars, 1); }
    
    if (findnocase('upper', charSet)) { usableChars = listappend(usableChars, 2); }
    
    if (len(usableChars) is 0) { usableChars = '1,2,3'; }

    if(listlen(strLen) gt 1 and listfirst(strLen) lt listlast(strLen)) { strLen = randrange(listfirst(strLen), listlast(strLen)); }
    else if (val(strLen) is 0) { strLen = 8; }
    
    
    while (len(tmpStr) LE strLen-1)
    {
        thisSet = listFirst(usableChars);
        usableChars = listdeleteat(usableChars, 1);
        usableChars = listappend(usableChars, thisSet);
    
        tmpStr = tmpStr & chr(randrange(listfirst(charSets[thisSet]), listlast(charSets[thisSet])));
    }
    
    for (i=1; i lte strLen; i=i+1)
    {
        thisCharPos = randrange(1, len(tmpStr));
        thisChar = mid(tmpStr, thisCharPos, 1);
        tmpStr = removeChars(tmPStr, thisCharPos, 1);
        newstr = newstr & thisChar;
    }
    
    return newStr;
}
</cfscript>

Search CFLib.org


Latest Additions

John Bartlett John Bartlett added
browserDetect
4 day(s) ago

Rob Brooks-Bilson Rob Brooks-Bilson added
listCompare
7 day(s) ago

Stephen Withington Stephen Withington added
formToNameValueP...
15 day(s) ago

anthony petruzzi anthony petruzzi added
parseExcel
20 day(s) ago

Pablo Varando Pablo Varando added
returnRandomHEXC...
21 day(s) ago

Top Rated

Nathan Dintenfass                                 QueryStringChang...
Rated 5.0, 10 time(s)

Stephen Withington formToNameValueP...
Rated 5.0, 5 time(s)

Gyrus                                             HTMLSafe
Rated 5.0, 4 time(s)

Shlomy Gantz                                      viewCSS
Rated 5.0, 4 time(s)

Michael Sharman generateRandomKe...
Rated 5.0, 3 time(s)

Created by Raymond Camden / Design by Justin Johnson