Question

In: Computer Science

Write a function election(numVoters) that simulates an election between two candidates A and B, with numVoters...

Write a function election(numVoters) that simulates an election between two candidates A and B, with numVoters voters, in which each voter randomly chooses candidate A 55% of the time. Return the fraction (between 0 and 1) of the voters who actually voted for candidate A in your simulation. finished in python

Solutions

Expert Solution

code in python (code to copy)

from random import choices

# This function election(numVoters) simulates 
# an election between two candidates A and B, 
# with numVoters voters, in which each voter 
# randomly chooses candidate A 55% of the time
def election(numVoters):
    # candidates in election
    candidates = ['A', 'B']
    # given in problem description that
    # people choose A 55% of the time
    # hence we create this distribution
    distribution = [0.55, 0.45]
    votes_for_A = 0
    # loop through number of voters to check their actual vote
    for i in range(numVoters):
        if(choices(candidates, distribution)[0]=='A'):
            votes_for_A = votes_for_A + 1
    # return the fraction of people who voted for A
    return votes_for_A/numVoters


# Lets do a simulation with 1 million voters
votes_for_A = election(1000000)
print(votes_for_A)

Python code screenshot

Output screenshot

Let me know in the comments if you have any doubts.
Do leave a thumbs up if this was helpful.


Related Solutions

Two candidates A and B are running for election. There are 5 voters who vote for...
Two candidates A and B are running for election. There are 5 voters who vote for one of the two candidates (no abstention). The candidate with most votes wins. A majority of voters prefer candidate A over candidate B. a) Describe formally this environment as a game (players, strategies, payoffs) b) Show that to vote for your preferred candidates is a weakly dominant strategy. c) Show that there exist Nash equilibria in which candidate B gets elected and that most...
QUESTION 1 Imagine you just had an election between two candidates: Abby and Tanya. There are...
QUESTION 1 Imagine you just had an election between two candidates: Abby and Tanya. There are 10,000 voters. After you count each vote, you calculate the percent of the vote Abby has so far. (In other words, you record the percent of counted voters who voted for Abby after one vote is counted, then after a second vote is counted, then after a third vote is counted, etc.) If you graphed Abby's percent of support after each vote counted, with...
Two candidates are competing in a majority rule election with 7 voters. The possible policies are...
Two candidates are competing in a majority rule election with 7 voters. The possible policies are ordered on a number line and creatively labeled {1, 2, 3, 4, 5, 6, 7}. Each policy is the favorite of one voter, and each voter has single peaked preferences. The candidates, L and R, announce policies, and whomever gets the most votes wins and implements the policy she announced. (Each voter votes for whichever candidate they strictly prefer. If a voter is indifferent,...
Write a function called play_round that simulates two people drawing cards and comparing their values. High...
Write a function called play_round that simulates two people drawing cards and comparing their values. High card wins. In the case of a tie, draw more cards. Repeat until someone wins the round. the function has two parameters: the name of player 1 and the name of player 2. it returns a string with format ' wins!'. For instance, if the winning player is named Rocket, return 'Rocket wins!'. python
In python please write the following code the problem. Write a function called play_round that simulates...
In python please write the following code the problem. Write a function called play_round that simulates two people drawing cards and comparing their values. High card wins. In the case of a tie, draw more cards. Repeat until someone wins the round. The function has two parameters: the name of player 1 and the name of player 2. It returns a string with format '<winning player name> wins!'. For instance, if the winning player is named Rocket, return 'Rocket wins!'.
Write a function matchSuit() that simulates repeatedly drawing a pair of playing cards from a shuffled...
Write a function matchSuit() that simulates repeatedly drawing a pair of playing cards from a shuffled deck (of 52 cards) until the pair of cards share the same suit (or you run out of cards). The function should return the total number of cards drawn. Use a while loop. To draw each card, call the drawCard function you wrote above.
The given table shows the preference schedule for an election with 5 candidates. Find the complete...
The given table shows the preference schedule for an election with 5 candidates. Find the complete ranking using the method of pairwise comparisons.​ (Assuming that ties are broken using the results of the pairwise comparisons between the tying​ candidates.) NUMBER OF VOTERS 11 9 7 5 4 3 1ST C E D B D B 2ND A A C E B C 3RD D B E C A E 4TH E D B A E D 5TH B C A...
Arlan needs to choose between the two candidates that he had for a job as the...
Arlan needs to choose between the two candidates that he had for a job as the new Manager in his new carwash branch. Jane is currently due for a promotion in her current company and is earning $42,000 yearly. She is willing to relocate, but does not tolerate working during weekends as she has 3 small children at home. She has a very good leadership and interpersonal skills. On the other hand, Betts who is very good in finance earns...
The voting paradox (10 points) Consider an election with three candidates, George, Lee, and Ray. There...
The voting paradox (10 points) Consider an election with three candidates, George, Lee, and Ray. There are 12 voters with the following preferences: Number of Voters Best Second Last 5 G L R 4 R G L 3 L R G In the plurality voting, where 12 voters vote for the three candidates and each voter is allowed to vote for only one candidate, who will become the winner? How many voters actually prefer G over R and how many...
Suppose 2 candidates are vying for election by trying to position themselves along a discrete political...
Suppose 2 candidates are vying for election by trying to position themselves along a discrete political spectrum 0 1 2 3 4 5 6 7 8 9 . Ten percent of the votes are at each location on the spectrum. Each candidate wants to maximize her share of the votes by choosing her position on the spectrum; voters vote for the candidate closest to their position on the spectrum, and if there is a tie in distance they split their...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT