1) Write the MATLAB code to generate a random number between 1 and 3 using the r
ID: 3600854 • Letter: 1
Question
1) Write the MATLAB code to generate a random number between 1 and 3 using the randi function. You can find more information about randi at http://www.mathworks.com/help/matlab/ref/randi.html.
2) Consider the Monty Hall problem (Those not familiar can find it here: http://en.wikipedia.org/wiki/Monty_Hall_problem or watch the following video until 1:07 http://www.youtube.com/watch?v=mhlc7peGlGg ).
a) Write the pseudo-code to ask the user which door they would like to initially choose. Also write the code to pick the winning door at random (Hint: store these in variables named initial_choice and winning_door).
b) Based on your solution to part A, Write the pseudo-code to determine which door is revealed to the user as a goat. Store the revealed door number in a variable named revealed_door.
c) Write the pseudo-code for the final decision to ask if the user wants to change doors, and then reveal whether or not they won a car or a goat. Store their response in a variable named change_door. You can expect the user’s response to be a ‘y’ for yes, and an ‘n’ for no. Determining whether they win or lose will depend on the initial_choice, winning_door and change_door values.
Note: MATLAB syntax for excluding elements of a list is as follows:
x = [5, 4, 3, 2, 1]
y = x(x~=4)
Where the variable y is now equal to [5, 3, 2, 1].
Explanation / Answer
1)
x= randi(3)
This code will generate a random number between 1 and 3.
2)
a)
inital_choice=input("Enter the choice of door")
winning_door=randi(3);
b)
inital_choice=input("Enter the choice of door ")
winning_door=randi(3);
doors=[3 2 1]
goat=doors(doors~=winning_door)
revealed_door=goat(1)
c)
initial_choice=input("Enter the choice of door ")
winning_door=randi(3);
doors=[3 2 1]
goat=doors(doors~=winning_door)
revealed_door=goat(1)
change_choice=input('wanna change the door','s');
option = "n";
tf = strcmp(change_choice,option)
if tf == 1
change_door = initial_choice
else
change_door =input('enter the new choice of door ')
end
if change_door == winning_door
disp("You won the car ")
else
disp("You lost ")
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.