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.

6 次查看(过去 30 天)
  2 个评论
Guillaume
Guillaume 2019-7-15
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 2019-7-15
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...

请先登录,再进行评论。

采纳的回答

Guillaume
Guillaume 2019-7-15
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 个评论
Guillaume
Guillaume 2019-7-15
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 2019-7-15
编辑:Md Jilani 2019-7-15
Here is my another problem link:
https://www.mathworks.com/matlabcentral/answers/471744-how-do-i-get-the-area-of-this-problem?

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by