Skip to content

Instantly share code, notes, and snippets.

@Crawron
Created May 5, 2021 19:29
Show Gist options
  • Select an option

  • Save Crawron/4d003a2d144074d26e6eb33dcd3bf3a9 to your computer and use it in GitHub Desktop.

Select an option

Save Crawron/4d003a2d144074d26e6eb33dcd3bf3a9 to your computer and use it in GitHub Desktop.
function simulatePoll(limit) {
const votes = [0, 0]
for (let i = 0; i < limit; i++) {
const rgn = Math.floor(Math.random() * 100 + 1)
if (rgn <= 1) votes[0] = votes[0] + 1
else votes[1] = votes[1] + 1
}
return votes
}
function runSimulation(polls, votes) {
console.log(`Simulating ${polls} polls of ${votes} votes each`)
for (let i = 0; i < polls; i++) {
const [one, nineNine] = simulatePoll(votes)
console.log({
poll: i,
one,
nineNine,
ratio: `${
((one / votes) * 100).toFixed(1)
} : ${
((nineNine / votes) * 100).toFixed(1)
}`,
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment