Outcomes e Random numbers while loops . Input validation . Running simulations P
ID: 3725982 • Letter: O
Question
Outcomes e Random numbers while loops . Input validation . Running simulations Part 1 Let's Make a Deal is a TV game show that Monty Hall (a U of M alumni!) hosted from 1963-1986. During the show, one contestant would be presented with 3 doors, where a prize was hidden behind one of the doors. The game went like this: 1·The contestant was asked to choose one door (without revealing what was behind) 2. Monty would reveal what was behind a different door, showing that there was no prize 3·The contestant would then choose whether to stay with door they originally chose, or to switch to the other remaining door but if they switch, their odds of winning are Confoundingly, if the contestant chooses to stick with their first choice, their odds of winning are . This is a problem that's popularly called-meMonty. Hallproblem. Monty Hall Problem - Numberphi You can also take a look at the Wikipedia article for a comprehensive description of the problem, along with visual and arithmetic explanations as to why the odds work out the way they do. If you're still not convinced, there's an entire playlist of videos by Numberphile covering the problemExplanation / Answer
Monty-Hall //Let's Make A Deal Game
For single trial:
#!/usr/bin/env python3
import argparse, random
def simulate(num_doors, switch, verbose):
win_door = random.randint(0, num_doors-1)
if verbose:
print('Thier is a surprize the behind door {}'.format(win_door+1))
choice = random.randint(0, num_doors-1)
if verbose:
print('Participant chooses door {}'.format(choice+1))
closed_doors = list(range(num_doors))
while len(closed_doors) > 2:
door_to_rem = random.choice(closed_doors)
if door_to_rem == win_door or door_to_rem == choice:
continue
closed_doors.remove(door_to_rem)
if verbose:
print('Participant opens door {}'.format(door_to_rem+1))
assert len(closed_doors) == 2
if switch:
if verbose:
print('Participant switches from door {} '.format(choice+1))
avail_doors = list(closed_doors)
avail_doors.remove(choice)
choice = avail_doors.pop()
if verbose:
print('to {}'.format(choice+1))
won = (choice == win_door)
if verbose:
if won:
print('Participant WON')
else:
print('Participant LOST')
return won
def main():
parser = argparse.ArgumentParser(
description='Monty Hall Game')
parser.add_argument('--doors', default=3, type=int, metavar='int',
help='Doors offered to the participant')
parser.add_argument('--trials', default=1, type=int, metavar='int',
help='Trials to perform')
parser.add_argument('--verbose', default=True, action='store_true',
help='Results of each trial')
args = parser.parse_args()
print('Simulating {} trials...'.format(args.trials))
winning_non_switchers = 0
winning_switchers = 0
for i in range(args.trials):
won = simulate(args.doors, switch=False, verbose=args.verbose)
if won:
winning_non_switchers += 1
won = simulate(args.doors, switch=True, verbose=args.verbose)
if won:
winning_switchers += 1
print(' Switching won {0:5} times out of {1} ({2}% of the time)'.format(
winning_switchers, args.trials,
(winning_switchers / args.trials * 100 ) ))
print('Not switching won {0:5} times out of {1} ({2}% of the time)'.format(
winning_non_switchers, args.trials,
(winning_non_switchers / args.trials * 100 ) ))
if __name__ == '__main__':
main()
----------------------------------------------------------------------------------------------------------------------------------------
Random Output:
Simulating 1 trials...
Thier is a surprize the behind door 2
Participant chooses door 1
Participant opens door 3
Participant LOST
Thier is a surprize the behind door 1
Participant chooses door 1
Participant opens door 3
Participant switches from door 1
to 2
Participant LOST
Switching won 0 times out of 1 (0% of the time)
Not switching won 0 times out of 1 (0% of the time)
-----------------------------------------------------------------------------------------------------------------
For multiple trials (Let trials=3)
parser.add_argument('--trials', default=3, type=int, metavar='int',
help='Trials to perform')
Note: Code will remain same just default trial will be "3"(in this case)
Note: You may change the code and take input from the user about simulation.
--------------------------------------------------------------------------------------------------------------------------------------------------
In Monty Hall it’s better to switch, which wins the prize more often.
If you switch, you have a 2/3 probability of winning the prize;
if you don’t switch, you’ll only win the car 1/3 of the time.
-------------------------------------------------------------------------------------------------------------------
To change the output precision you may use different data type.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.