Last active
October 11, 2015 17:48
-
-
Save pixelrevision/3896610 to your computer and use it in GitHub Desktop.
Unique random numbers javascript
This file contains 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
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