Created
April 12, 2020 05:25
-
-
Save RickyCook/decf9201b9c5b629a1e19d63234af6c2 to your computer and use it in GitHub Desktop.
Random pairs generation
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
// sortedHashes = ['a', 'b', 'c', 'd', 'e', ...] | |
// doneRaces = [['a', 'c'], ['c', 'e'], ['a', 'b'], ...] | |
function getNewVoteRace(sortedHashes, doneRaces) { | |
let firstHash; | |
let secondIdxMax; | |
let secondTotalOpts; | |
let secondBlacklist; | |
while(_.isNil(secondIdxMax) || secondBlacklist.length >= secondTotalOpts) { | |
const firstIdxMax = sortedHashes.length - 2; | |
const firstIdxRand = getRandomInt(firstIdxMax); | |
const firstIdx = firstIdxRand; | |
firstHash = sortedHashes[firstIdx]; | |
secondIdxMax = sortedHashes.length - 1 - firstIdx - 1; | |
secondTotalOpts = secondIdxMax + 1; | |
secondBlacklist = doneRaces | |
.filter(([f,s]) => f === firstHash) | |
.map(([f,s]) => s); | |
} | |
let secondHash; | |
while(_.isNil(secondHash) || secondBlacklist.indexOf(secondHash) !== -1) { | |
const secondIdxRand = getRandomInt(secondIdxMax); | |
const secondIdx = sortedHashes.length - 1 - secondIdxRand; | |
secondHash = sortedHashes[secondIdx]; | |
} | |
return [firstHash, secondHash]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment