Using the bar Function

Right now I am working on a Monte Carlo Sim for rolling for dice. I have figured out the first part of the problem but need help finishing it.
-use simulation to find the probability that the sum is 13
-use simulation to find the probability that all four dice are 3
What I cant figure out is this last part...
-use bar function to graph the percentages of the number elements to get the sum of four dice:4, 5,6,....,23,24
My code so far...
close all;
clear all;
clc;
count13 = 0;
count3 = 0;
%% Roll 4 dice 50000 times
for i=1:5000;
dice1 = randi(6, 1);
dice2 = randi(6, 1);
dice3 = randi(6, 1);
dice4 = randi(6, 1);
sum = dice1 + dice2 + dice3 + dice4;
%% Count the #13's rolled
if sum == 13;
count13 = count13 + 1;
end
%% Count the times all dice were rolled on 3
if dice1 == 3 && dice2 == 3 && dice3 == 3 && dice4 == 3
count3 = count3 + 1;
end
end
fprintf('The probability of rolling a 13 is %.3f percent\n',(count13/5000)*100);
fprintf('The probability of rolling all dice on 3 is %.3f percent\n',(count3/5000)*100);

回答(2 个)

Rik
Rik 2018-11-26
编辑:Rik 2018-11-26
Your code can be improved in many ways, but I kept your code as it was in the block below. You shouldn't shadow the sum function. Using function names for your variables is a frequent source of errors. The most common examples are sum and input.
close all;
clearvars;%don't use clear all, it makes for slow code
%also, don't use clearvars outside of debugging
clc;
count13 = 0;
count3 = 0;
count_array=zeros(4*6,1);
%% Roll 4 dice 50000 times
for i=1:5000
dice1 = randi(6, 1);
dice2 = randi(6, 1);
dice3 = randi(6, 1);
dice4 = randi(6, 1);
sum_val = dice1 + dice2 + dice3 + dice4;%don't shadow the sum function
%% Count the #13's rolled
if sum_val == 13
count13 = count13 + 1;
end
%% Count the times all dice were rolled on 3
if dice1 == 3 && dice2 == 3 && dice3 == 3 && dice4 == 3
count3 = count3 + 1;
end
%% Count the number of times a specific sum happened
count_array(sum_val)=count_array(sum_val)+1;
end
fprintf('The probability of rolling a 13 is %.3f percent\n',(count13/5000)*100);
fprintf('The probability of rolling all dice on 3 is %.3f percent\n',(count3/5000)*100);
figure(1),clf(1)
bar(count_array)
In the block below I have put an example of how you can do this without a loop and with a bit more flexibility if you want to change the number of rolls or the number of dice.
rolls=50000;N_dice=4;
dice_vals=randi(6,N_dice,rolls);
is13=sum(dice_vals,1)==13;
count13=sum(is13);
is_all3=all(dice_vals==3,1);
count3=sum(is_all3);
sum_vals=sum(dice_vals,1);
edges=0.5:(0.5+max(sum_vals));centers=edges(2:end)-0.5;
%or:
%edges=0.5:(0.5+6*N_dice);centers=edges(2:end)-0.5;
N = histcounts(sum_vals,edges);
figure(1),clf(1)
bar(centers,N)
fprintf('The probability of rolling a 13 is %.3f percent\n',(count13/rolls)*100);
fprintf('The probability of rolling all dice on 3 is %.3f percent\n',(count3/rolls)*100);

1 个评论

Did this suggestion solve your problem? If so, please consider marking it as accepted answer. It will make it easier for other people with the same question to find an answer. If this didn't solve your question, please comment with what problems you are still having.

请先登录,再进行评论。

Close, but why not vectorize it instead of using loops? You can make all the rolls simultaneously then just count them with sum(). Try this:
% Roll 4 dice 50000 times
numExperiments = 5000
% Roll all 4 dice "numExperiments" times.
all4Dice = randi(6, numExperiments, 4);
% Count the #13's rolled
sumWithinRows = sum(all4Dice, 2); % Sum all 4 dice for all experiments.
count13 = sum(sumWithinRows == 13);
fprintf('13 occurred in %d experiments\n', count13);
fprintf('The probability of rolling a 13 is %.3f percent\n',(count13/numExperiments)*100);
% Count the times all 4 dice were a 3
num3sPerRow = sum(all4Dice == 3, 2);
count3 = sum(num3sPerRow == 4);
fprintf('Four 3s occurred in %d experiments\n', count3);
fprintf('The probability of rolling all dice on 3 is %.3f percent\n',(count3/numExperiments)*100);
You'll see something like
13 occurred in 503 experiments
The probability of rolling a 13 is 10.060 percent
Four 3s occurred in 7 experiments
The probability of rolling all dice on 3 is 0.140 percent

Community Treasure Hunt

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

Start Hunting!

Translated by