CFLib.org – Common Function Library Project

randStr(strLen, charset)

Last updated April 2, 2007

author

Bobby Hartsfield

Version: 2 | Requires: CF5 | Library: StrLib

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:

/**
 * 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;
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Simon Bingham added
convertSecondsTo...
April 15, 2015

Umbrae added
fullUrlEncode
February 11, 2015

Created by Raymond Camden / Design by Justin Johnson