JSFiddle - React, Tailwind, and code Playground
JavaScript
var UniqueNumber = (function() {
'use strict';
function UniqueNumber(min, max) {
this._cache = new Map();
this._min = min;
this._length = max - min + 1;
}
UniqueNumber.prototype = {
get: function() {
if (!this._length) {
return NaN;
}
var cache = this._cache;
var value = Math.floor(Math.random() * this._length);
this._length--;
var mapValue = cache.get(value);
var mapValueL = cache.get(this._length);
cache.set(value, mapValueL == null ? this._length : mapValueL);
return (mapValue == null ? value : mapValue) + this._min;
}
};
return UniqueNumber;
})();
var uniqInt = new UniqueNumber(0, 8);
for (var i = 0; i < 12; ++i) {
console.log(uniqInt.get());
}