45 views (last 30 days)

My task is to write a function that simulates rolling a pair of 6-sided dice n times and uses this data to approximate probabilities of each of the possible outcomes of sums of the dice (2-12) and create a vertical bar graph that displays the frequency of each outcome. I have a solid program written up, however I am wondering if this program does what it should do and if it could be more efficient in some way. Here is what I have:

function prob = ex4(n)

%

%

rng('shuffle')

prob = zeros(1,12);

TotalNRolls = 0;

while TotalNRolls <= n

die(1) = randi(6);

die(2) = randi(6);

sum = die(1) + die(2);

TotalNRolls = TotalNRolls + 2;

if sum == 2

prob(2) = TotalNRolls/n;

elseif sum == 3

prob(3) = TotalNRolls/n;

elseif sum == 4

prob(4) = TotalNRolls/n;

elseif sum == 5

prob(5) = TotalNRolls/n;

elseif sum == 6

prob(6) = TotalNRolls/n;

elseif sum == 7

prob(7) = TotalNRolls/n;

elseif sum == 8

prob(8) = TotalNRolls/n;

elseif sum == 9

prob(9) = TotalNRolls/n;

elseif sum == 10

prob(10) = TotalNRolls/n

elseif sum == 11

prob(11) = TotalNRolls/n;

else

prob(12) = TotalNRolls/n;

end

end

bar(prob)

jessupj
on 27 Oct 2020

Edited: jessupj
on 27 Oct 2020

rng('shuffle')

N = 1000; % total number of rolls you want

rollz=NaN(N,1);

for k=1:N;

rollz(k) = sum( randi([1 6],2,1) ); % add two uniiformly distr'd random ints from {1,2,3,4,5,6}

end

bar(rollz)

hist(rollz)

Rik
on 27 Oct 2020

Rik
on 27 Oct 2020

The Matlab way would be to use an array. If you read the documentation for randi you will see how you can generate a matrix with the integers 1 to 6.

If you then take a look at the documentation for the sum function, you can see how to sum either the rows or the column. That way you can calculate the sums of all your rolls at once.

The last step is then to get a bar chart of the distribution. You can use the bar function, but you can als look at other tools that allow you to make a histogram.

A final note: try to avoid using sum as a variable name, because that means you can't use the builtin function sum. (you should also avoid i, j, lowercase L, o, and O as variable names)

Steven Lord
on 27 Oct 2020

function prob = ex4(n)

%

%

rng('shuffle')

prob = zeros(1,12);

TotalNRolls = 0;

while TotalNRolls <= n

Conceptual question: what is n? Is it the number of dice you roll (in which case n = 3 would require rolling the pair of dice twice or require you to stop after your first roll lest you go over the number of dice you're allowed to roll?) Or is it the number of times you roll the pair of dice (in which case n = 3 would require rolling a total of six dice?)

die(1) = randi(6);

die(2) = randi(6);

Rather than calling randi twice, consider calling it once to get a 1-by-2 (or a 2-by-1) vector of random numbers. Take a look at the randi documentation, it contains examples showing how to generate more than one number at a time.

It also seems a bit odd to me to store the results of rolling a pair of dice (plural) in a variable named die (singular.)

sum = die(1) + die(2);

As Rik suggested, don't use sum as a variable name.

TotalNRolls = TotalNRolls + 2;

if sum == 2

prob(2) = TotalNRolls/n;

elseif sum == 3

prob(3) = TotalNRolls/n;

I wouldn't do the division inside the loop. I would just count how many times each result occurred inside the loop and divide by the number of rolls afterwards.

Also note that the lines of code in your if / elseif / else block look awfully similar. Do you see a pattern in the sum of the two rolled dice and which element of your probability vector needs to be updated? That whole if / elseif / else block can be collapsed to one line.

Opportunities for recent engineering grads.

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

Start Hunting!
## 1 Comment

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/627958-roll-a-6-sided-die-and-compute-the-probability-of-the-sum-with-bar-graph#comment_1090583

⋮## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/627958-roll-a-6-sided-die-and-compute-the-probability-of-the-sum-with-bar-graph#comment_1090583

Sign in to comment.