Ah thanks Steve.
So is 97 to 123 the placement of upper and lowercase letters and the numbers on the CharCode chart?
and are the upper and lowercase letters 32 letters apart on the chart?
Trying to understand where you got these values. Your solution is very good and interesting to me. Also, why random off of 62? Is that the total characters in the array alphaNumeric?
Thank you for this.

Karl


On Jun 17, 2010, at 12:57 AM, Steven Sacks wrote:

RegEx isn't really used for what you're talking about.

You should use ascii codes to create your alphanumeric array and then choose random indexes from it to create your random string.

var i:int;
var alphaNumeric:Array = [];
for (i = 97; i < 123; ++i)
{
    alphaNumeric.push(String.fromCharCode(i));
    alphaNumeric.push(String.fromCharCode(i - 32));
}
for (i = 0; i < 10; ++i)
{
    alphaNumeric.push(i);
}
function getRandomString(i:int):String
{
    var str:String = "";
    while (i--)
    {
        str += alphaNumeric[Math.round(Math.random() * 62)];
    }
    return str;
}

trace(getRandomString(8));
-- lKzU4e0X

It's worth pointing out that Math.random() is merely decent at generating random values. If you really need random values, it is better to use something like the Parker Miller pseudo-random number generator.

http://www.gskinner.com/blog/archives/2008/01/source_code_see.html
_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Karl DeSaulniers
Design Drumm
http://designdrumm.com

_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to