let Identity = (function () {
let instantiated;
let spin = 0;
let mapping = [
'2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R',
'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
];
function init() {
spin = Math.floor(Math.random() * 4095);
return {
generate: function (type, room_id) {
let millisecond = (new Date()).getTime();
let current_spin = spin++;
if (current_spin > 4095) {
spin = 0;
current_spin = spin++;
}
let a = millisecond.toString(2).padStart(42, '0');
let b = current_spin.toString(2).padStart(12, '0');
let c = type.toString(2).padStart(4, '0');
let d = room_id.toString(2).padStart(32, '0');
let s = a + b + c + d;
let ret = [];
for (let i = 0; i < 90;) {
ret.push(mapping[parseInt(s.substr(i, 5), 2)]);
i += 5;
}
return ret.join('');
}
};
}
return {
getInstance: function () {
if (!instantiated) {
instantiated = init();
}
return instantiated;
}
}
})();
Identity.getInstance();
let arr = [];
let un = new Map();
for (let i = 0; i < 4096 * 10; i++) {
let tmp = Identity.getInstance().generate(1, 2);
un.set(tmp, true);
arr.push(tmp);
}
console.log(un.size === arr.length ? '沒有重複' : '有重複');
for (let i = 0; i < arr.length; i++) {
if (i + 1 < arr.length && arr[i] > arr[i + 1]) {
console.log('異常的大小', arr[i], arr[i + 1]);
}
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結