Created
November 6, 2018 02:23
-
-
Save harryh/91209772322fec0e71bd5ede7ccd9ece to your computer and use it in GitHub Desktop.
This file contains 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
import random | |
from collections import Counter | |
NUM_DISTRICTS = 500 | |
# Doesn't matter, think of each of these as representing | |
# thousands of voters if you like. | |
NUM_VOTERS = 1000 | |
# % of voters that vote D (remainder vote R) | |
DEM_PERCENT = .51 | |
NUM_D = int(NUM_DISTRICTS * NUM_VOTERS * DEM_PERCENT) | |
NUM_R = int(NUM_DISTRICTS * NUM_VOTERS * (1 - DEM_PERCENT)) | |
VOTERS = (['D'] * NUM_D) + (['R'] * NUM_R) | |
# No gerrymandering, so shuffle voters at random. | |
random.shuffle(VOTERS) | |
district_winners = Counter() | |
for i in range(0, NUM_VOTERS * NUM_DISTRICTS, NUM_VOTERS): | |
# Count the votes in district i | |
votes = Counter(VOTERS[i:i+NUM_VOTERS]) | |
# print(votes) | |
# Find the winner | |
winner = votes.most_common()[0][0] | |
district_winners[winner] += 1 | |
print(district_winners) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment