Give this a shot Richard.
Each time it loops to grab the next character, it grabs it from the next
usable character set in the list so it uses them all.
Just so it's not the same sequence of character sets over and over, the
second loop rebuilds the string by pulling out random characters one at a
time and appending them into a new string.
All my tests have been successful so far. Let me know how yours turn out. If
everything works out for you as well, I'll submit it.
There is only one thing that is not so random about the string... if you use
a length that is evenly divisible by the number of character sets that you
use, then there will be an even number of each character set in the
resulting string.
Say you used randstr(9, 'alphanumericupper') (3 character sets alpha,
numeric, and upper)... the result would have 3 lowercase letters, 3
uppercase letters and 3 numbers. I don't think that is a deal breaker but
just thought I'd point it out.
<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 ([EMAIL PROTECTED])
* @version 2, January 25, 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>
--
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.5.432 / Virus Database: 268.17.10/651 - Release Date: 1/24/2007
6:48 PM
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Upgrade to Adobe ColdFusion MX7
Experience Flex 2 & MX7 integration & create powerful cross-platform RIAs
http:http://ad.doubleclick.net/clk;56760587;14748456;a?http://www.adobe.com/products/coldfusion/flex2/?sdid=LVNU
Archive:
http://www.houseoffusion.com/groups/CF-Talk/message.cfm/messageid:267626
Subscription: http://www.houseoffusion.com/groups/CF-Talk/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4