Created
May 30, 2011 00:46
-
-
Save rafaelrinaldi/998295 to your computer and use it in GitHub Desktop.
Get a random string with alphanumeric and numeric characters.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode 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