Created
May 5, 2021 19:29
-
-
Save Crawron/4d003a2d144074d26e6eb33dcd3bf3a9 to your computer and use it in GitHub Desktop.
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
| 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