Hey, so sorry this took so long. Here's the Octave code, and the rest of the answers are in the file.
Code:
% John O. Woods
% HW #1, P03
% Bill Press' CS 395T
function roll = die_roll(sides)
roll = floor(rand(1) * sides);
end
function [monty,other] = monty_picks_a_goat(car, goat1, goat2, first_guess)
d = die_roll(2)
if first_guess == car
if d == 0
monty = goat1
other = goat2
else
monty = goat2
other = goat1
end
elseif first_guess == goat1
monty = goat2
other = goat1
elseif first_guess == goat2
monty = goat1
other = goat2
else
% Only works in Matlab, not in Octave:
% errRecord = MException('VerifyInput:OutOfBounds', 'Inputs are outside the allowable limits');
% throw(errRecord);
printf("Error: Goats are problematic\n");
first_guess
goat1
goat2
quit
end
end
function win_with_change = game_show()
car = 1;
goat1 = 0;
goat2 = 2;
monty_goat = -1;
other_goat = -1;
first_choice = die_roll(3)
[monty_goat,other_goat] = monty_picks_a_goat(car, goat1, goat2, first_choice);
if first_choice == car
win_with_change = 0;
else
win_with_change = 1;
end
end
function [rate_win_change, rate_win_same, record_wins] = monty_carlo(trials)
record_wins = zeros(trials,1)
win_change = 0;
win_same = 0;
for n = 1:trials
% Give the trial:
printf("------------- Trial: ");
n
result = game_show()
if result == 1
win_change++;
else
win_same++;
end
record_wins(n,1) = win_change / n;
end
printf("------------- End\n");
plot(record_wins)
printf("Rate when we change choices: ");
rate_win_change = win_change / trials
printf("Rate when we keep the same choice: ");
rate_win_same = win_same / trials
end