Question: Monty Hall Simulation Provided below is some template of code that you can build on to complete the function. As you fill in the code,

Monty Hall Simulation
Provided below is some template of code that you can build on to complete the function. As you fill in the code,
uncomment the lines of code. I have left you some hints along the way. You may approach the problem in a different way if
you would like, but here is how I approached it.
Code
monty = function(strat = c("stay", "switch", "random"), N){
#N is number of iterations
#strat = 'stay', 'switch', 'random' is the strategy you use.win =0 #to keep track of number of winsprize=sample(1:3, size =1) #randomize which door has the good prizeReveal one of the doors you didn't pick which has a bum prizereveal = #Insert Code. This line should assign to `reveal' the door that is neither the prreveal = #Insert Code. This line should sample randomly from the two doors that are the no
}if(strat == 'switch'){}select = #Insert Code. This line should assign to `select' the guess.if(strat == 'random'){}if(select == prize){} if(strat == 'switch'){
20 # select = #Insert Code. This line should assign to `select' the door that is neither the one
21 # }
22 # if(strat == 'stay'){
23 # select = #Insert Code. This line should assign to `select' the guess.
24 # }
25 # if(strat == 'random'){}if(select == prize){}paste('Using the ',strat,' strategy, your win percentage was ',Print the win percentage of your strategy
#}
#monty(strat = "stay", 10000)
#monty(strat = "switch", 10000)
#monty(strat = "random", 10000)
The following uses Daphna's solution to provide the correct answers. You should test your own function to see if you get
similar answers.
Code
1}\mathrm{ monty(strat = "stay", 10000)
2}\mathrm{ monty(strat = "switch", 10000)
3 monty(strat = "random", 10000)
[1] "Using the stay strategy, your win percentage was 33.58%"
[1] "Using the switch strategy, your win percentage was 66.9%"
[1] "Using the random strategy, your win percentage was 50.1%"
Monty Hall Simulation Provided below is some

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!