Skip to content

Instantly share code, notes, and snippets.

@rafaelrinaldi
Created May 30, 2011 00:46
Show Gist options
  • Save rafaelrinaldi/998295 to your computer and use it in GitHub Desktop.
Save rafaelrinaldi/998295 to your computer and use it in GitHub Desktop.
Get a random string with alphanumeric and numeric characters.
package tea.string
{
/**
*
* Get a random string with alphanumeric and numeric characters.
*
* @param p_length String length.
*
* @author Rafael Rinaldi (rafaelrinaldi.com)
* @since May 29, 2011
*
*/
public function randomString( p_length : int = 0 ) : String
{
if(p_length <= 0) return "";
const CHARS_TABLE : String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
const NUMBERS_TABLE : String = "0123456789";
var string : String = "";
var flag : Boolean;
while(string.length < p_length) {
const table : String = flag ? CHARS_TABLE : NUMBERS_TABLE;
const index : int = int(Math.random() * table.length);
string += table.charAt(index);
flag = Boolean((Math.random() * 100 | 0) % 2);
}
return string;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment