# Dice Rolling probability (matlab)

1. Aug 4, 2014

### Maylis

1. The problem statement, all variables and given/known data
In this problem, we will demonstrate the Central Limit Theorem by a virtual test that involves
rolling of dice. To this end, you will create a function, with the following declaration line
Code (Text):
function [avgDice, histDice] = rollDice(NumDice, NumRolls)
$\bullet$ NumDice: the number of dice that will be rolled in each experiment, and
$\bullet$ NumRolls: the number of times the experiment will be performed (i.e. the number of times
the dice are rolled).

The output arguments are

$\bullet$ avgDice: a scalar that is the average of the sum of the dice values from each experiment, and
$\bullet$ histDice: a 6*numDice-by-1 array where the i-th element is an integer that corresponds
to the number of experiments that the total value of the dice is equal to i. So, for example
if histDice(4) = 12, this means that the sum of the dice was equal to 4 for 12 of the
experiments.

Important Note: Use the randi command in Matlab to simulate rolling the dice. Specifically, to
be compatible with the autograder you must use the command randi(6,NumDice,NumRolls).
The function should also generate a histogram that displays the distribution of the results. Each
bin in the histogram should represent a possible value for the sum of the dice. For example, if
NumDice = 1, the bins should span the values 1 to 6. If NumDice = 2 the bins should range
from 2 to 12. To complete this plotting task, use the MATLAB function bar.

Test your function with many different sets of inputs including the following:
(a) NumDice = 1 and NumRolls = 500
(b) NumDice = 3 and NumRolls = 500
(c) NumDice = 10 and NumRolls = 500
(d) NumDice = 10 and NumRolls = 5000
(e) NumDice = 10 and NumRolls = 50000
Notice the effect of the number of dice and the number of rolling experiments on the shape of
the resulting distribution. You should notice that as the number of rolls increases the distribution
approaches the shape of the normal distribution.

2. Relevant equations

3. The attempt at a solution
Code (Text):
function [avgDice, histDice] = rollDice(NumDice, NumRolls)
DiceOutCome = randi(6,NumDice,NumRolls);
avgDice = mean(sum(DiceOutCome));

histDice = zeros(6*numDice,1);
for i = 1:6*NumDice
histDice(i) = sum(DiceOutCome(:,i));
end
I am having particular difficulty understanding what is meant from

histDice: a 6*numDice-by-1 array where the i-th element is an integer that corresponds
to the number of experiments that the total value of the dice is equal to i. So, for example
if histDice(4) = 12, this means that the sum of the dice was equal to 4 for 12 of the
experiments.

Since I don't know what it means, I can't put it into code form, but what I wrote is what I think it is saying. I am not sure though.

EDIT: based off my new understanding, this is my code for that section
Code (Text):
for 1:6*NumDice
for j = 1:6
histDice(i) = numel(find(sum(DiceOutCome) == j));
end
end

Last edited: Aug 4, 2014
2. Aug 4, 2014

### Maylis

I got the code right, now I just need to do the histogram.

I am having problem with the plot
Code (Text):
binranges = NumDice:6*NumDice;
[bincounts] = histc(histDice,binranges);
figure;
bar(binranges,bincounts,'histc');
And for some reason, the plots are capping in the y-axis as 1. Also, it just doesn't look right at all.

EDIT: got it to work now

Last edited: Aug 4, 2014