Dice Rolling probability (matlab)

Click For Summary
The discussion focuses on implementing a MATLAB function to simulate dice rolling and demonstrate the Central Limit Theorem. The function, `rollDice(NumDice, NumRolls)`, calculates the average sum of dice rolls and generates a histogram of the results. Users encountered challenges in understanding how to populate the `histDice` array, which tracks the frequency of each possible sum. After some iterations, the code was refined to correctly compute the histogram, although initial plotting issues were noted, particularly with y-axis limits and appearance. Ultimately, the user resolved the plotting problems, successfully completing the task.
gfd43tg
Gold Member
Messages
947
Reaction score
48

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

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:
Physics news on Phys.org
I got the code right, now I just need to do the histogram.

I am having problem with the plot
Code:
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:

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 53 ·
2
Replies
53
Views
9K
Replies
6
Views
558
  • · Replies 16 ·
Replies
16
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 19 ·
Replies
19
Views
4K
  • · Replies 13 ·
Replies
13
Views
3K