- #1

gfd43tg

Gold Member

- 950

- 50

## Homework Statement

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:

`function [avgDice, histDice] = rollDice(NumDice, NumRolls)`

##\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.

## Homework Equations

## The Attempt at a Solution

Code:

```
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:

```
for 1:6*NumDice
for j = 1:6
histDice(i) = numel(find(sum(DiceOutCome) == j));
end
end
```

Last edited: