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!'.
In Python write a function that simulates the throwing of any even numbered dice which will...
In Python write a function that simulates the throwing of any even numbered dice which will be passed on as a parameter: customDice(sides). The function will return a random number rolled for that particular type of dice. Write the program that will utilize the customDice function with the following specification: Ask the user to pick which even-sided dice to use. The program will then roll the dice twice. If the total of both rolls is greater than the number of...
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.
Write a Matlab function called “SimpleChannel” which takes a signal as an input, simulates a channel...
Write a Matlab function called “SimpleChannel” which takes a signal as an input, simulates a channel and returns the transmitted signal.. SimpleChannel function will add a randomly generated noise (look for “rand” function) to the signal and return it. You can adjust the amplitude of the noise as you want in order to have a distinct noise effect. Then write another matlab code to show the channel noise. First create the signal and then simulate the channel by using your...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT