Write a program to calculate the probability of two fair dice having sum equal to 7. There are two fair dice and we are interested in seeing whether the sum of the two fair dice having sum equal to 7. Use Monte Carlo Simulation.

15 views (last 30 days)
  2 Comments
Guillaume
Guillaume on 15 Jul 2019
You've posted the text of your homework, but haven't actually asked a question.
Hopefully, you're not asking us to do your homework for you (which we won't do).
Md Jilani
Md Jilani on 15 Jul 2019
Sorry sir... This is my first post in this site and that was my fault that I didn't ask any question. Also this is not my homework. This was the question of previous trimester on my school. I tried something on that but I can't get the exact result or I am wrong with my concept. Thats why I didn't post with my tried code. Here it is...
count = 0;
p=1/6;
ntrials = 1000;
sumseven=zeros(ntrials,1);
nAll = 0;
n7 = 0;
prob = 0;
%for probability
for throw1 = 1:6
for throw2 = 1:6
nAll = nAll + 1;
if throw1 + throw2 == 7
n7 = n7 + 1;
end
end
end
%for count
for i=1:ntrials
throws1 = randi(6, 1);
throws2 = randi(6, 1);
sumThrow = throws1 + throws2; % Do not use "sum" as name of a variable
if sumThrow == 7
fprintf('True Sum is 7\n')
count = count + 1;
else
fprintf('false\n')
end
end
fprintf('Number of throws with sum=7 is %d time\n', count);
prob = ((n7 / nAll)*count)/ntrials;
disp(prob);
I am sorry for my activites anyway...

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 15 Jul 2019
Everything is fine with your code (although the calculation of n7 and nAll could be done with no loop at all), except for your calculation of prob where for some reason you multiply two probabilities together, the theoretical probabilty (n7/nAll) and your monte-carlo probability (count/ntrials), which of course doesn't make much sense.
  3 Comments
Guillaume
Guillaume on 15 Jul 2019
If you have another problem then start a new a question, giving as many details as possible.
If this solution solve your current problem to your liking then please accept it.
Md Jilani
Md Jilani on 15 Jul 2019
Edited: Md Jilani on 15 Jul 2019
Here is my another problem link:
https://www.mathworks.com/matlabcentral/answers/471744-how-do-i-get-the-area-of-this-problem?

Sign in to comment.

More Answers (0)

Categories

Find more on Mathematics in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!