Created
January 16, 2016 19:24
-
-
Save bmoren/a829c21e89fa249ab5a4 to your computer and use it in GitHub Desktop.
Non Repeating Random Number Utility, with loop callback and end-to-end elimination
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
| /* | |
| Create a new nonRepRand instance like this, you can have multiple, just use a diff variable name: | |
| var numberGenerator = new nonRepRand(1,5); | |
| use the .next() method to get the next number, see example below | |
| .next() method, can take a callback which will fire when the nonRepRand generator loops around the set, ie. if nonRepRand(1,5), .next(callBack) will fire the callback after generating it's 5th value, the callback will be passed a value of true. | |
| nonRepRand() will never give you end to end repeating numbers. for example if the 5th value of the set is a "3" and the first value of the next loop is a "3", nonRepRand will automatically get you a new value instead of a "3", so the values can never be next to one another. | |
| */ | |
| var nonRepRand = function(low, high){ | |
| function range(start, end) { | |
| var foo = []; | |
| for (var i = start; i <= end; i++) { | |
| foo.push(i); | |
| } | |
| return foo; | |
| } | |
| function shuffle(o){ //v1.0 - google labs | |
| for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x); | |
| return o; | |
| } | |
| this.low = low; | |
| this.high = high; | |
| this.numberSet = shuffle(range( this.low, this.high)); | |
| this.pop | |
| this.prev | |
| // console.log(this.numberSet); | |
| this.next = function(loopCallback){ | |
| this.prev = this.pop; | |
| this.pop = this.numberSet.shift() | |
| if(this.pop == undefined){ //have we reached the end? | |
| if(loopCallback != null){ | |
| loopCallback(true); | |
| } | |
| this.numberSet = shuffle(range( this.low, this.high)); //get a new numberset | |
| // console.log(this.numberSet); | |
| this.pop = this.numberSet.shift() //get the first val from the new numberset | |
| if(this.pop == this.prev){ //is the first val of the new numberset the same as the previous? | |
| this.numberSet.push(this.pop) //move it to the front of the array | |
| // console.log("moved " + this.pop + " to back of the array"); | |
| this.pop = this.numberSet.shift() //get the second value instead | |
| } | |
| } | |
| return this.pop; | |
| } | |
| } | |
| /* +~+~+~+~+~+~+~+~+~+~+~+~+~+~+ Render some thigs to the Console +~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+ */ | |
| var numberGenerator = new nonRepRand(1,5); //create a new nonRepRand generator | |
| setInterval(function(){ | |
| console.log( numberGenerator.next(cb) ); | |
| }, 600) | |
| function cb(loop){ | |
| console.log("Loopcallback working! " + loop); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment