Generate 32 bit random number sixteen hex string

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

Some time ago, the need for a unique identification code when doing business, so I thought of a class UIDUtil AS3.0 itself, it has a static method in a createUID. To have a look inside the API description:

createUID () Method
public static function createUID():String
Languages: ActionScript 3
Product version: Flex 3
The runtime version: Flash Player 9, AIR 1.1
Pseudo random number generator ActionScript and the current time generation based on UID (unique identifier).
UID type " XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX", where X is a sixteen hexadecimal numbers (0-9, A-F).
The UID is not the only truly global, but this is not the best method to generate UID player supports the case.
Returns the new generation String - UID.

public static function createUID():String
    {
        var uid:Array = new Array(36);
        var index:int = 0;
       
        var i:int;
        var j:int;
       
        for (i = 0; i <8; i++)
        {
            uid[index++] = ALPHA_CHAR_CODES[Math.floor(Math.random() *  16)];
        }

        for (i = 0; i <3; i++)
        {
            uid[index++] = 45; // charCode for "-"
           
            for (j = 0; j <4; j++)
            {
                uid[index++] = ALPHA_CHAR_CODES[Math.floor(Math.random() *  16)];
            }
        }
       
        uid[index++] = 45; // charCode for "-"

        var time:Number = new Date().getTime();
        // Note: time is the number of milliseconds since 1970,
        // which is currently more than one trillion.
        // We use the low 8 hex digits of this number in the UID.
        // Just in case the system clock has been reset to
        // Jan 1-4, 1970 (in which case this number could have only
        // 1-7 hex digits), we pad on the left with 7 zeros
        // before taking the low digits.
        var timeString:String = ( "0000000" + time.toString(16).toUpperCase()).substr(-8);
       
        for (i = 0; i <8; i++)
        {
            uid[index++] = timeString.charCodeAt(i);
        }
       
        for (i = 0; i <4; i++)
        {
            uid[index++] = ALPHA_CHAR_CODES[Math.floor(Math.random() *  16)];
        }
       
        return String.fromCharCode.apply( null, uid);
    }

There is a String.fromCharCode.apply in the last line of code (null, uid); function class like this sentence; Java inside the StringBuffer into String, this is a way of the highest string operation efficiency. For example, in AS splicing SQL, in this way is much higher than the connection string.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Gustave at December 11, 2013 - 11:40 AM