fabio revised this gist . Go to revision
1 file changed, 4 insertions, 3 deletions
Venezuelan_election_odds.py
| @@ -3,9 +3,9 @@ | |||
| 3 | 3 | >>> N = 10_058_774 | |
| 4 | 4 | # Counter of occurrences | |
| 5 | 5 | >>> cnt = 0 | |
| 6 | - | # Counts how many occurrences we measure of share of | |
| 7 | - | # votes that match their rounded representation to the | |
| 8 | - | # first decimal digit | |
| 6 | + | # Count how many occurrences we measure where the | |
| 7 | + | # shares of votes exactly match their rounded | |
| 8 | + | # representation to the first decimal digit | |
| 9 | 9 | >>> for _ in range(N): | |
| 10 | 10 | # Share of votes to candidate A, | |
| 11 | 11 | # as a random number between 30% and 70% | |
| @@ -26,5 +26,6 @@ | |||
| 26 | 26 | ... if A == A_round and B == B_round and C == C_round: | |
| 27 | 27 | ... cnt += 1 | |
| 28 | 28 | ... | |
| 29 | + | # Print the counter after >10M iterations | |
| 29 | 30 | >>> cnt | |
| 30 | 31 | 0 | |
fabio revised this gist . Go to revision
1 file changed, 30 insertions
Venezuelan_election_odds.py(file created)
| @@ -0,0 +1,30 @@ | |||
| 1 | + | >>> from random import randint | |
| 2 | + | # Number of voters | |
| 3 | + | >>> N = 10_058_774 | |
| 4 | + | # Counter of occurrences | |
| 5 | + | >>> cnt = 0 | |
| 6 | + | # Counts how many occurrences we measure of share of | |
| 7 | + | # votes that match their rounded representation to the | |
| 8 | + | # first decimal digit | |
| 9 | + | >>> for _ in range(N): | |
| 10 | + | # Share of votes to candidate A, | |
| 11 | + | # as a random number between 30% and 70% | |
| 12 | + | ... A = randint(round(0.3 * N), round(0.7 * N)) / N | |
| 13 | + | # Share of votes to candidate C (others), | |
| 14 | + | # as a random number between 1% and 10% | |
| 15 | + | ... C = randint(round(0.01 * N), round(0.1 * N)) / N | |
| 16 | + | # Share of votes to candidate B, | |
| 17 | + | # 100% - votes(A) - votes(C) | |
| 18 | + | ... B = 1 - A - C | |
| 19 | + | # Get the values of A, B and C rounded to the | |
| 20 | + | # first decimal | |
| 21 | + | ... A_round = round(round(A/N, 3) * N) | |
| 22 | + | ... B_round = round(round(B/N, 3) * N) | |
| 23 | + | ... C_round = round(round(C/N, 3) * N) | |
| 24 | + | # If the rounded representations of A, B and C | |
| 25 | + | # match their actual values, increase the counter | |
| 26 | + | ... if A == A_round and B == B_round and C == C_round: | |
| 27 | + | ... cnt += 1 | |
| 28 | + | ... | |
| 29 | + | >>> cnt | |
| 30 | + | 0 | |