Skip to content

Instantly share code, notes, and snippets.

@franckc
Created January 27, 2020 19:50
Show Gist options
  • Save franckc/cb78cc9220e5c1c47d3790b6658c1124 to your computer and use it in GitHub Desktop.
Save franckc/cb78cc9220e5c1c47d3790b6658c1124 to your computer and use it in GitHub Desktop.
const seedrandom = require('seedrandom')
const fs = require('fs')
const numWinners = 30
// Read hashes from a file. They are expected to be in block descending order (most recent first).
const hashes = fs.readFileSync('hashes.txt').toString().trim().split('\n')
// Seed a random generator with the most recent hash.
const randomGenerator = seedrandom(hashes[0])
// Draw the winners, making sure to exclude duplicates.
const seen = {}
let index
console.log(`Out of ${hashes.length} participants, the ${numWinners} lucky winners are:`)
for (let i=1; i <= numWinners; i++) {
do {
index = Math.floor(randomGenerator() * hashes.length)
} while (seen[index])
console.log(`#${i}:\t${hashes[index]}`)
seen[index] = true
}
@rusmani03
Copy link

rusmani03 commented Mar 12, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment