From 1d6ec55612533a0f6182cb24d032528c44936b0e Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Sat, 13 Apr 2024 20:37:55 -0400 Subject: [PATCH] add 2010 census results --- data/electoral-college-votes-2010-census.csv | 52 ++++++++++++++++++++ data/electoral-college-votes.source | 1 + main.go | 16 +++--- 3 files changed, 62 insertions(+), 7 deletions(-) create mode 100644 data/electoral-college-votes-2010-census.csv diff --git a/data/electoral-college-votes-2010-census.csv b/data/electoral-college-votes-2010-census.csv new file mode 100644 index 0000000..6a88085 --- /dev/null +++ b/data/electoral-college-votes-2010-census.csv @@ -0,0 +1,52 @@ +State,Votes +Alabama,9 +Alaska,3 +Arizona,11 +Arkansas,6 +California,55 +Colorado,9 +Connecticut,7 +Delaware,3 +District of Columbia,3 +Florida,29 +Georgia,16 +Hawaii,4 +Idaho,4 +Illinois,20 +Indiana,11 +Iowa,6 +Kansas,6 +Kentucky,8 +Louisiana,8 +Maine,4 +Maryland,10 +Massachusetts,11 +Michigan,16 +Minnesota,10 +Mississippi,6 +Missouri,10 +Montana,3 +Nebraska,5 +Nevada,6 +New Hampshire,4 +New Jersey,14 +New Mexico,5 +New York,29 +North Carolina,15 +North Dakota,3 +Ohio,18 +Oklahoma,7 +Oregon,7 +Pennsylvania,20 +Rhode Island,4 +South Carolina,9 +South Dakota,3 +Tennessee,11 +Texas,38 +Utah,6 +Vermont,3 +Virginia,13 +Washington,12 +West Virginia,5 +Wisconsin,10 +Wyoming,3 diff --git a/data/electoral-college-votes.source b/data/electoral-college-votes.source index c261536..029a177 100644 --- a/data/electoral-college-votes.source +++ b/data/electoral-college-votes.source @@ -1 +1,2 @@ https://www.archives.gov/electoral-college/allocation +https://www.npr.org/2021/04/26/983082132/census-to-release-1st-results-that-shift-electoral-college-house-seats diff --git a/main.go b/main.go index b12f5d5..0da4515 100644 --- a/main.go +++ b/main.go @@ -24,14 +24,14 @@ type VotesForEachParty struct { // type src = *rand.Rand /* Globals */ -var r = rand.New(rand.NewPCG(uint64(1), uint64(2))) +var r = rand.New(rand.NewPCG(uint64(100), uint64(2224))) /* Load data from csvs */ func readStates() ([]State, error) { var states map[string]State = make(map[string]State) /* Electoral college votes for the 2024 election*/ - votes_file, err := os.Open("data/electoral-college-votes.csv") + votes_file, err := os.Open("data/electoral-college-votes-2010-census.csv") if err != nil { return nil, fmt.Errorf("error opening the votes file: %v", err) } @@ -64,7 +64,6 @@ func readStates() ([]State, error) { if err != nil { return nil, fmt.Errorf("error opening the results file for %s: %v", year, err) } - defer results_file.Close() resultsReader := csv.NewReader(results_file) if _, err := resultsReader.Read(); err != nil { // Skip header return nil, fmt.Errorf("error reading results header for %s: %v", year, err) @@ -83,6 +82,8 @@ func readStates() ([]State, error) { data.VictoriousPartyPerElection[year] = party states[state] = data } + + results_file.Close() } // Convert statesData map to a slice for returning @@ -103,7 +104,7 @@ func sampleFromState(state State) VotesForEachParty { // 2012: R // 2016: R // 2020: Split, 1 D, 4 R - p_split := float64(2 / 6) + p_split := 2.0 / 6.0 if r.Float64() < p_split { return VotesForEachParty{Democrats: 1, Republicans: 4} } else { @@ -116,7 +117,7 @@ func sampleFromState(state State) VotesForEachParty { // 2012: D // 2016: Split: 3 D, 1 R // 2020: Split, 3 D, 1 R - p_split := float64(2 / 6) + p_split := 2.0 / 6.0 if r.Float64() < p_split { return VotesForEachParty{Democrats: 3, Republicans: 1} } else { @@ -158,7 +159,8 @@ func simulateElection(states []State) int { } // fmt.Printf("\nDemocrat seats: %d\n", republican_seats) - if republican_seats > 270 { + fmt.Printf(" (%d) ", republican_seats) + if republican_seats >= 270 { return 1 } else { return 0 @@ -176,7 +178,7 @@ func main() { n_sims := 100_000 p_republicans := 0.0 - for _ = range n_sims { + for i := 0; i < n_sims; i++ { result := simulateElection(states) fmt.Printf("Election result: %d\n", result) if result == 1 {