2007-06-22

Randomize letras

Continuando meu pensamento sobre asc, quando fui interrompido...
Vi uma pergunta sobre randomizar letras, já deve existir em algum lugar, mas desenvolvi a minha.


<cffunction name="randChar">
<cfscript>
var qchar = QueryNew("") ;
var qchar2 = '' ;
var char1 = 65 ;
var char2 = 122 ;
var char3 = 0 ;
if(StructKeyExists(ARGUMENTS,"1")) char1 = Asc(ARGUMENTS['1']);
if(StructKeyExists(ARGUMENTS,"2")) char2 = Asc(ARGUMENTS['2']);
QueryAddColumn(qchar,"c",ListToArray("65,66,67,68,69,70,71,72,73,
74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,97,98,99,100,
101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,
117,118,119,120,121,122"));
if(char1 gt char2){
char3 = char1;
char1 = char2;
char2 = char3;
}
</cfscript>
<cfquery name="qchar2" dbtype="query">
select c from qchar
where c >= #char1# and c <= #char2#
</cfquery>
<cfif NOT qchar2.recordcount>
<cfreturn Chr(qchar.c[RandRange(1,qchar.recordcount)])>
</cfif>
<cfreturn Chr(qchar2.c[RandRange(1,qchar2.recordcount)])>
</cffunction>
<cfoutput>#randChar("c","P")#</cfoutput>

Nenhum comentário: