CFLib.org – Common Function Library Project

randStr(strLen, charset)

Last updated April 2, 2007

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

 
Rated 7 time(s). Average Rating: 4.4

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:

view plain print about
<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:

view plain print about
<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>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

CF Ninja CF Ninja added
calcIRR
10 day(s) ago

Stephen Withington Stephen Withington added
getWeekOfMonth
10 day(s) ago

Adam Cameron Adam Cameron added
createPrimeNumbe...
a while ago

Ray Ford Ray Ford added
timeZoneNow
a while ago

Top Rated

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 48 time(s)

Barney Boisvert indentXml
Rated 5.0, 12 time(s)

Rachel Lehman deAccent
Rated 5.0, 9 time(s)

Darwan Leonardo Sitepu splitNumber
Rated 5.0, 8 time(s)

Created by Raymond Camden / Design by Justin Johnson