I wrote a random number generator for CFLib.org called getRandomNumber
<http://cflib.org/udf.cfm?ID=1329>, but it only returns numbers. :o( you
can however pass it any length (in digits) that you want and you'll get
a number back with that many digits. I like the idea of being able to
tell the UDF what character set to use. Good Idea. I don't see you
seeding the random number generator. Check out my udf (the link above)
and see what I'm doing for seeding. My original version had that all
screwed up and my numbers weren't very random at all... :o( My buddy,
Steven worked on that bit with me and I think the solution we came up
with is pretty cool.
Bobby Hartsfield wrote:
> 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>
>
>
>
--
http://www.cjordan.info
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
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:264832
Subscription: http://www.houseoffusion.com/groups/CF-Talk/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4