A p l a y e r ro lls t w o d i ce . Eac h d ie h a s s i x f ac e s . T h e s e
ID: 3537900 • Letter: A
Question
A player rolls two dice. Each die has six faces. These faces contain 1, 2, 3, 4, 5, and 6 spots. After the dice have come to rest, the sum of the spots on the two upward faces is calculated.
%u2022 If the sum is 7 or 11 on the first throw, the player wins.
%u2022 If the sum is 2, 3, or 12 on the first throw (called %u201Ccraps%u201D), the player loses (i.e. the %u201Chouse%u201D wins).
%u2022 If the sum is 4, 5, 6, 8, 9, or 10 on the first throw, then the sum becomes the player's %u201Cpoint.%u201D
o To win, you must continue rolling the dice until you %u201Cmake your point.%u201D The player loses by rolling a 7 before making the point.
Write a program to simulate a game of craps, resulting in a win or loss. Make sure your program displays appropriate messages so that the user can follow each step of the game.
Run the game multiple times to estimate the probability that the player wins. For example, if the player wins
490 out of 1000 games, then the estimated probability of winning is 490/1000 = 0.49. Run the game at least
5000 times.
Explanation / Answer
function main
%play 5000 times
wins=0;
for i=1:5000
wins = wins + game();
end
prob = wins/5000;
fprintf('Probability of winning is : %f ',prob);
function win=game()
roll1=floor((rand()*6)+1);
roll2=floor((rand()*6)+1);
point=0;
win=0;
sum=roll1+roll2;
if sum==7 || sum==11
win=1;
elseif sum==2 || sum==3 || sum==12
win=0;
else
point=sum;
while(true)
roll1=floor((rand()*6)+1);
roll2=floor((rand()*6)+1);
sum=roll1+roll2;
if sum == point
win=1;
break;
elseif sum== 7
win=0;
break;
end
end
end
end
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.