Ok... here it is. I just submitted it to cflib.org so it should be up there
soon under the name 'randStr'. I checked the processing time for a bit and
couldn’t get anything over 0 until I started generating strings 200+ in
length.

The two required params are strLen and charSet

strLen: can either be a single digit like 8 which would generate an 8
character string every time. OR it can be a comma delimited list of 2
numbers such as 8,16 which would result in a random length between 8 and 16
characters.

charSet: can be any combination of the three words alpha, numeric and upper
that you can come up with.

EXAMPLES:
Alpha
Alphanumeric
Alphanumericupper
Alphaupper
Etc...

Alpha: a-z
Numeric: 0-9
Upper: A-Z


I did not add a lower option since you can just leave upper out and achieve
that.

If you use upper, you will have a mix of uppercase and lowercase characters.
If you want only upper, wrap the final output in ucase().

You COULD use 'numeric' as the charset but that will just result in a random
number at the specified length... there are better ways to do that.

I did not put in special characters because I don’t like them :-)
Actually, special characters are spread out in the ascii ranges so they are
a pain, I may add them later when time permits. It shouldn't be too hard. I
basically just have to take the time to pick out the ranges and add them to
the array. 

===================================================
Example use: #randstr('8,16', 'alphanumericupper')#
===================================================

<cfscript>
function randStr(strLen, charSet)
{
        var usableChars = '';
        var charSets = arrayNew(1);
        var tmpStr = '';        
        
        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; }
        
        for (i=1; i lte strLen; i=i+1)
        {
                if (listlen(usableChars) gt 1)
                { 
                        Set = listgetat(usableChars, randrange(1,
listlen(usableChars)));
                        thisChar = chr(randrange(listfirst(charSets[Set]),
listlast(charSets[Set])));
                        tmpStr = tmpStr & thisChar;
                }
                else
                { 
                        Set = usableChars;
                        thisChar = chr(randrange(listfirst(charSets[Set]),
listlast(charSets[Set])));
                        tmpStr = tmpStr &
chr(randrange(listfirst(charSets[usableChars]),
listlast(charSets[usableChars]))); 
                }
        }
        return tmpStr;
}
</cfscript>


-- 
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.5.432 / Virus Database: 268.15.26/594 - Release Date: 12/20/2006
3:54 PM
 



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Create robust enterprise, web RIAs.
Upgrade & integrate Adobe Coldfusion MX7 with Flex 2
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:264827
Subscription: http://www.houseoffusion.com/groups/CF-Talk/subscribe.cfm
Unsubscribe: 
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.4

Reply via email to