ruk·si

🍦 JavaScript
UUID

Updated at 2015-05-05 04:49
/**
 * @return {string} Version 4 universally unique identifier,
 * a 36 lowercase character string with dashes.
 */
var generateUuid = function () {
    var d = new Date().getTime();
    var pattern = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx';
    var uuid = pattern.replace(/[xy]/g, function (c) {
        var r = ((d + Math.random() * 16) % 16) | 0;
        d = Math.floor(d / 16);
        return ((c == 'x') ? r : (r & 0x3 | 0x8)).toString(16);
    });
    return uuid;
};
console.assert(generateUuid() !== generateUuid());
// d250b046-b7ea-4f55-8ef6-c23e97bd7aeb
// cf2c3dd0-a2f4-461a-bd39-6fb34777663e

Sources