Write a program to simulate the flipping of three fair coins. Compute the number
ID: 3528441 • Letter: W
Question
Write a program to simulate the flipping of three fair coins. Compute the number of times that all three coins come up the same in 1000 trials, and compare to the number of times you would expect. You may need to useExplanation / Answer
function avgflips=coinflip(initP1,initP2,initP3,p,n) X =(rand(3,1)>p); count=zeros(n,1); for i=1:n playercoins=zeros(3,1); playercoins(1,1)=initP1; playercoins(2,1)=initP2; playercoins(3,1)=initP3; c=1; j=1; while j>0 if X(1,1)==X(2,1) && X(2,1)==X(3,1) playercoins=playercoins; elseif X(1,1)~=X(2,1) && X(1,1)~=X(3,1) && X(2,1)==X(2,1) playercoins=playercoins+[2;-1;-1]; elseif X(1,1)~=X(2,1) && X(1,1)==X(3,1) && X(2,1)~=X(3,1) playercoins=playercoins+[-1;2;-1]; elseif X(1,1)==X(2,1) && X(2,1)~=X(3,1) && X(1,1)~=X(3,1) playercoins=playercoins+[-1;-1;2]; end if playercoins(1,1)==0 j=0; elseif playercoins(2,1)==0 j=0; elseif playercoins(3,1)==0 j=0; end c=c+1; end count(i)=c; end avgflips=mean(count); end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.