Last active
April 29, 2016 10:40
-
-
Save kopiro/7a1cdfb8ef46da2de9eb9356b779d151 to your computer and use it in GitHub Desktop.
Random fixed sequence repetion
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
| // this value define how many entropy add | |
| var ENTROPY_ITERATION = 1; | |
| // how many times multiply the array | |
| var MULTIPLIER = 1000000; | |
| // the output array | |
| var A = []; | |
| // the matrix generator | |
| var M = [0,1,2,3,4,5,6,7,8,9]; | |
| // stats | |
| var STATS = { | |
| startTime: Date.now(), | |
| endTime: null, | |
| entropy: { | |
| success: 0, | |
| skipped: 0 | |
| } | |
| }; | |
| // we save our forbidden value for the cycle | |
| var previousRandomIndex = 0; | |
| // Cycle N times | |
| for (var i = 0; i < MULTIPLIER; i++) { | |
| // generate an array of indexex from 0 to M.length | |
| var randomIndexes = M.map(Number.call, Number); | |
| // but temporary exclude "the one" (we add it again after first cycle) | |
| randomIndexes.splice(previousRandomIndex, 1); | |
| // check if is our first cycle | |
| var inBoundaryLeft = true; | |
| // pop elements in random order while the random array is empty | |
| while (randomIndexes.length > 0) { | |
| // casual number from 0 to L(RI) | |
| var randomNumber = Math.floor((Math.random() * randomIndexes.length)); | |
| // casual (UNIQUE) index | |
| var randomIndex = randomIndexes.splice(randomNumber, 1).pop(); | |
| // if we are in a bound left, readd the "one" cause we already extracted it | |
| if (inBoundaryLeft) { | |
| randomIndexes.push(previousRandomIndex); | |
| inBoundaryLeft = false; | |
| } | |
| // save the value for the next turnaround | |
| previousRandomIndex = randomIndex; | |
| // extract and push | |
| A.push( M[randomIndex] ); | |
| } | |
| } | |
| // now, to ensure no-preditect distribution, | |
| // fuck the system adding more entropy! | |
| for (var e = 0; e < ENTROPY_ITERATION; e++) { | |
| for (var i = 1; i < A.length - M.length - 1; i += Math.ceil(Math.random() * 2)) { | |
| // random offset | |
| var maxLen = Math.min(10, A.length - M.length - 1 - i); | |
| var randomIndexAdd = Math.floor(Math.random() * maxLen); | |
| // new index to swap | |
| var j = i + M.length + randomIndexAdd; | |
| if (j < A.length) { | |
| // if the "couple" rule is respected, so swap, otherwise we can safely skip! | |
| if ( | |
| A[i] !== A[j] && | |
| A[i-1] !== A[j] && A[i+1] !== A[j] && | |
| A[j-1] !== A[i] && A[j+1] !== A[i] | |
| ) { | |
| STATS.entropy.success++; | |
| var tmp = A[i]; | |
| A[i] = A[j]; | |
| A[j] = tmp; | |
| } else { | |
| STATS.entropy.skipped++; | |
| } | |
| } | |
| } | |
| } | |
| STATS.endTime = Date.now(); | |
| STATS.diff = STATS.endTime - STATS.startTime; | |
| // Check if the couple rule is respected | |
| for (var i = 1; i < A.length; i++) { | |
| if ( +A[i-1] == +A[i] ) { | |
| console.log('Failed ', A.slice(i-3, i+3)); | |
| throw 'err'; | |
| } | |
| } | |
| console.log( STATS ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment