Skip to content

Instantly share code, notes, and snippets.

@RickyCook
Created April 12, 2020 05:25
Show Gist options
  • Save RickyCook/decf9201b9c5b629a1e19d63234af6c2 to your computer and use it in GitHub Desktop.
Save RickyCook/decf9201b9c5b629a1e19d63234af6c2 to your computer and use it in GitHub Desktop.
Random pairs generation
// 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