Question

In: Computer Science

The Monty Hall problem solved on Matlab. Please show code. You are given a choice between...

The Monty Hall problem solved on Matlab. Please show code. You are given a choice between three doors. Behind one door is a new car and behind the other two doors is a goat. You first pick one of the doors at random. Then, a door with a goat is opened for you. You are then given the option to switch your door to the other unopened door. Should you do this? What are your odds of winning if you switch vs not switching? Show using matlab with a large number of runs, where you keep your selection 50% of the time and switch 50% of the time.

Solutions

Expert Solution

disp 'Let''s play the Monty Hall game!';
disp 'There is a car behind one of the three doors 1, 2, 3';

conti = "y";
switch_wins = 0;
not_switch_wins = 0;

% Iterate loop till user enters "y" for conti
while(conti!="n")
  
is_switched = false;
  
% Pick a number from 1,2,3
guess = input('Pick a door between 1, 2 or 3: ');

% Print the door user has picked
fprintf(['\nYou picked door number %d \n'],guess);

% Generate a random number for winning door
winningNumber = randi([1,3]);
  
% If user's guess is equal to winning door
if (guess == winningNumber)
% Generate a random number from the other two losing door
revealDoor = randi([1,3]);
  
% This loop is to make sure reveal door is not equal to winning door
while (revealDoor==winningNumber)
revealDoor = randi([1,3]);
end

% If user's guess is not equal to winning door
else
% Generate reveal door apart from user's guess and winning door
revealDoor = setdiff(1:3, [guess,winningNumber]);
end
  
% Print revealing door and print that it is a goat.
fprintf(['\nBefore we reveal what you have won, let''s see what you have lost behind door %d \n'],revealDoor);
fprintf('It''s a goat!\n\n');
  
% Generate remaining door and ask if they want to switch with this door
remainDoor = setdiff(1:3, [guess,revealDoor]);
choice = input(['Now, do you want to switch the door to ' num2str(remainDoor) '? [y/n] '],'s' );
  
% If they enter yes, switch it
if(choice == "y")
temp = guess;
guess = remainDoor;
remainDoor = temp;
is_switched = true;
end

% Now, check if user's guess is equal to winning number
% If yes, print congratualations
# Else, print the door number with car and that they have a won a goat
if(guess == winningNumber)
fprintf('\nCongratulations, you win the car!\n');
if(is_switched)
switch_wins +=1;
else
not_switch_wins+=1;
end
else
fprintf(['\nThe car is behind door #%d ,and You have won a goat!\n'],winningNumber);
end
  
% Ask if they want to play again
conti = input(['want to play again ? (y/n) '],'s' );
end

fprintf('Switched Wins = %d\n',switch_wins);
fprintf('Not Switched Wins = %d\n',not_switch_wins);

SCREENSHOT

OUTPUT



Related Solutions

(Monty Hall problem) Suppose you’re on a game show, and you’re given the choice of three...
(Monty Hall problem) Suppose you’re on a game show, and you’re given the choice of three doors, say Door 1, Door 2, and Door 3. Behind one door there is a car; behind the others, goats. Assume it is equally likely that the car is behind any door, i.e., P(D1) = P(D2) = P(D3). You will win whatever is behind the door you choose. (a) If you pick Door 1, what is your probability of winning the car? [2 point]...
Monty hall Problem Explain the statistical probabilities associated with the game show
Monty hall Problem Explain the statistical probabilities associated with the game show
The Monty Hall problem is a famous problem loosely based on the game show Let's Make...
The Monty Hall problem is a famous problem loosely based on the game show Let's Make a Deal. You are a contestant on the game show. There are 3 doors in front of you. Behind one door is a prize, and behind the other two doors are goats. Assume the door with the prize is picked uniformly at random from the three doors. First, you pick a door. Then, Monty Hall will open one of the other two doors that...
(a) In the Monty Hall problem with 100 doors, you pick one and Monty opens 98...
(a) In the Monty Hall problem with 100 doors, you pick one and Monty opens 98 other doors with goats. What is the probability of winning (assuming you would rather have a car than a goat) if you switch to the remaining door? Explain your answer. (b) Suppose Monte opens 98 doors without checking for cars. What is the probability that, once the doors are open, changing your choice will not change your chances of winning.
Matlab code for Gauss Siedel with solved example in Matlab
Matlab code for Gauss Siedel with solved example in Matlab
2. (Monty Hall) Suppose you are on a game show and are presented with three closed...
2. (Monty Hall) Suppose you are on a game show and are presented with three closed doors marked door 1, 2, and 3. Behind one door is a prize and behind the other two are goats. Suppose the host allows you to select one door, but the following two rules apply: • Before it is opened the host opens one of the two unselected doors that has a goat behind it. • The host then allows you to switch your...
1. (a) Consider a modified version of the Monty Hall problem. In this version, there are...
1. (a) Consider a modified version of the Monty Hall problem. In this version, there are 8 boxes, of which 1 box contains the prize and the other 7 boxes are empty. You select one box at first. Monty, who knows where the prize is, then opens 6 of the remaining 7 boxes, all of which are shown to be empty. If Monty has a choice of which boxes to open (i.e. if the prize is in the box you...
1. (a) Consider a modified version of the Monty Hall problem. In this version, there are...
1. (a) Consider a modified version of the Monty Hall problem. In this version, there are 8 boxes, of which 1 box contains the prize and the other 7 boxes are empty. You select one box at first. Monty, who knows where the prize is, then opens 6 of the remaining 7 boxes, all of which are shown to be empty. If Monty has a choice of which boxes to open (i.e. if the prize is in the box you...
This is a Matlab Exercise problem. Please create the Matlab code and figure for the following...
This is a Matlab Exercise problem. Please create the Matlab code and figure for the following problem using problem specifications: Plot x vs y when y=sin(x), y=cos(x), y=sin (2*x), and y=2*sin(x) when x = 1:0.1:10. Use 2 by 2 subplot, sin(x) is in location 1, cos(x) is in location 2, sin(2*x) is in location 3 and 2*sin(x) is in location 4. The plot should have: (1) x label = ‘x value’, y label = ‘y value’, legend ‘y=sin(x)’,’ y=cos(x)’,’ y=sin...
C++ questions about Monty Hall Problem, please make sure to divide the program into functions which...
C++ questions about Monty Hall Problem, please make sure to divide the program into functions which perform each major task. Imagine yourself on the set of a game show. You're given the choice of three doors. Behind one of the doors is a car you can drive home in if you guess correctly. Behind the other two doors are goats. After you initially guess the door, the host of the game show (who knows the door holding the car) opens...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT