Created
August 4, 2020 11:12
-
-
Save BlaM/f52b3ba6c1a8da0ee2c6e946ee32d0e4 to your computer and use it in GitHub Desktop.
random-id-generator.js
This file contains hidden or 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
function RandomIdGenerator() { | |
var self = this; | |
var lut = RandomIdGenerator.lut; | |
var myCounter = ++RandomIdGenerator.counter; | |
var idCounter = 0; | |
self.get = function() { | |
++idCounter; | |
var d0 = Date.now(); | |
var d1 = (idCounter & 0xffff) | (myCounter<<16 & 0xffff0000); | |
var d2 = Math.random()*0x100000000>>>0; | |
var d3 = Math.random()*0x100000000>>>0; | |
return lut[d0>>24&0xff]+lut[d0>>16&0xff]+lut[d0>>8&0xff]+lut[d0&0xff]+'-'+ | |
lut[d1>>8&0xff]+lut[d1&0xff]+'-'+ | |
lut[d1>>24&0x0f|0x40]+lut[d1>>16&0xff]+'-'+ | |
lut[d2&0x3f|0x80]+lut[d2>>8&0xff]+'-'+ | |
lut[d2>>16&0xff]+lut[d2>>24&0xff]+lut[d3&0xff]+lut[d3>>8&0xff]+lut[d3>>16&0xff]+lut[d3>>24&0xff]; | |
} | |
} | |
RandomIdGenerator.counter = 0; | |
RandomIdGenerator.lut = []; for (var i=0; i<256; i++) { RandomIdGenerator.lut[i] = (i<16?'0':'')+(i).toString(16); } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment