Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

write a script file to simulate 100 plays of a game in which you flip two coins.

ID: 3650879 • Letter: W

Question

write a script file to simulate 100 plays of a game in which you flip two coins. You win the game if you get two heads, lose if you get two tails, and flip again if you get one head and one tail. Create three user-defined functions to use in the script. Function flip simulates the flip of one coin, with the state s of the random number generator as the input argument, and the new state s and the result of the flip (0 for a tail and 1 for a head) as the outputs. Function flips simulates the flipping of two coins and calls flip. The inputof flips is the state s, and the outputs are the new state s and the result (0 for two tails, 1 for a head and a tail, and 2 for two heads). Function match simulates a turn at the game. its input is the state s and its outputs are the result (1 for win, 0 for lose) and the new state s. The script should reset the random number generator to its initial state, compute the state s, and pass this state to the user-defined functions

Explanation / Answer

Please rate...

Save it as coinFlip.m in the matlab working directory.

See screenshot below on how to run.

Program coinFlip.m

====================================================

function [c]=coinFlip()
c=0;
s=0;
for i=1:100
    a=match(s);
    if(a==1)
        c=c+1;
    end
end
disp('The number of wins(in a game of 100): ');
end


function s=flip(s)
s=round(rand(1,1));
end

function s=flips(s)
a=flip();
b=flip();
if(a==0 && b==0)s=0;
elseif(a==1 && b==1)s=2;
elseif(a==1 && b==0)s=1;
elseif(a==0 && b==1)s=1;
end
end

function s=match(s)
a=flips();
if(a==2)s=1;
elseif(a==0)s=0;
elseif(a==1)match();
end
end

===================================================

Sample output: