Answered You can hire a professional tutor to get the answer.

QUESTION

Write a program that will simulate the 2016 Presidential Election based on five swing states.

Write a program that will simulate the 2016 Presidential Election based on five swing states.

Numerous media outlets try to predict the likelihood of a particular result in 2016 election based upon taking the current polling data and running thousands of simulations. You are going to do a much smaller, simpler version of this type of simulation in this POTD. We will only use the values from five states along with the current likelihood of each candidate winning these states from fivethirtyeight.com.

Here is some code to start with 

# states are VA, FL, PA, CO, OH (in that order)state_chances_for_trump = [.238, .569, .304, .324, .634]electoral_votes_by_state = [13, 29, 20, 9, 18]

For example, based on the code above, Trump has a 23.8% chance of winning Virginia's 13 electoral votes. You can assume Clinton has the corresponding 76.2% chance - we will ignore third party candidates to keep this simple (sorry Gary Johnson fans!).

To properly simulate this, your program will need to generate a random number for each state for each run of the simulation you want to do. You will use random.random() to generate a decimal number between 0 and 1. If the number that is returned is less than or equal to the value given in the state_chances_for_trump list, then Trump wins the state and those electoral votes. Otherwise, Clinton wins the electoral votes. For example, consider Virginia again. Trump has a 23.8% chance of winning the state, or .238. random.random()will generate a number somewhere between 0 and 1. Thus, any number that happens to be between 0 and .238 represents a run that successfully hits within that 23.8% chance. Conversely, Clinton wins with any number greater than .238, representing her 76.2% chance.

Testing a program with a random number generator can be tricky. However, it is possible to ensure the same numbers come up each time by "seeding" the random number generator. When you seed a random number generator, it will then use that number as the basis to generate all of its random numbers. Using the same seed will give the same numbers each time. By default, random.random() uses the current system time as its seed. Your program should have the ability to be completely random or accept a seed for testing.

Your program should do the following:

  1. Prompt the user for how many simulations it should run.
  2. Prompt the user for anintto seed the random number generator. If the users provides a0, then the system should be completely random.
  3. The system should then create a random number for each of the five states and tally up the electoral votes for each candidate, picking a winner for that simulated election.
  4. Over the course of all simulated election runs, your program should keep track of how many election each candidate wins (by having the most electoral votes), to provide an overall percentage chance of winning the election. You calculate this by dividing the number of simulated elections a candidate wins by the total number of simulations you run.

To seed your random number generator, you can use code that looks like this:

seed = int(input("Enter a seed (0 for random): "))if seed != 0:random.seed(seed)

After this code is run, random.random() will generate numbers based upon either your seed or the current system time if the if statement is not triggered.

You do not need to do any extra formatting on any numbers used in this program.

An example run might be:

How many simulation runs?: 5Enter a seed (0 for random): 3Run 0: Trump wins with 60Run 1: Trump wins with 69Run 2: Clinton wins with 60Run 3: Clinton wins with 76Run 4: Trump wins with 47Chance of Trump winning: 0.6Chance of Clinton winning: 0.4

Another run might be:

How many simulation runs?: 7Enter a seed (0 for random): 13Run 0: Clinton wins with 71Run 1: Trump wins with 80Run 2: Trump wins with 49Run 3: Trump wins with 58Run 4: Clinton wins with 89Run 5: Clinton wins with 60Run 6: Trump wins with 47Chance of Trump winning: 0.5714285714285714Chance of Clinton winning: 0.42857142857142855

Another run might be:

How many simulation runs?: 5Enter a seed (0 for random): 0Run 0: Clinton wins with 51Run 1: Clinton wins with 51Run 2: Clinton wins with 47Run 3: Clinton wins with 51Run 4: Trump wins with 80Chance of Trump winning: 0.2Chance of Clinton winning: 0.8

You MUST use random.random() and random.seed() to generate your random numbers and seed so we can test your program effectively!

Show more
LEARN MORE EFFECTIVELY AND GET BETTER GRADES!
Ask a Question