mustcodemore.com

3.13.2006

AS 2.0 example of a unique id builder

//06.13.06
function GenerateString(strLng:Number)
{
var resultString = "";

//cheated a bit with the array, could do something more elegant to deal with case sensitivity
var alphabet:Array = new Array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j","k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" , "A" , "B", "C", "D", "E", "F", "G", "H","I", "J", "K", "L", "M", "N", "O", "P", "Q", "R" , "S", "T", "U", "V", "W", "X", "Y", "Z"
);
for (var i = 0; i < color="#333399">length
; ++i){
//first decide to make a # or a Letter
var tmp = Math.round(Math.ceil(Math.random()*2));
//1 = letter - pick uCase or lCase
if (tmp == 1) {
var j = Math.round(Math.ceil(Math.random()*alphabet.length - 1));
resultString += alphabet[j];

}
//2 = a number
if (tmp == 2) {
var k = Math.round(Math.ceil(Math.random()*9));
resultString += k;
}
//you could just return this to the calling method instead...

return resultString;
}
}

0 Comments:

Post a Comment

<< Home