Skip to content

Instantly share code, notes, and snippets.

@pixelrevision
Last active October 11, 2015 17:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pixelrevision/3896610 to your computer and use it in GitHub Desktop.
Save pixelrevision/3896610 to your computer and use it in GitHub Desktop.
Unique random numbers javascript
var UniqueRandom = function(min, max, increments){
this.min = min;
this.max = max;
if(increments === undefined){
this.increments = 1;
}else{
this.increments = increments;
}
this.reset();
};
UniqueRandom.prototype.get = function() {
if(this.uniques.length === 0){
this.reset();
}
var r = Math.floor(Math.random() * this.uniques.length);
var num = this.uniques[r];
this.uniques.splice(r, 1);
return num;
};
UniqueRandom.prototype.reset = function(){
this.uniques = [];
for(var i=this.min; i<this.max; i+=this.increments){
this.uniques.push(i);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment