# Dice Rolling probability (matlab)

• gfd43tg
In summary, the problem requires the creation of a function that simulates rolling dice and displays the distribution of the results in a histogram. The function takes in two inputs: the number of dice to be rolled and the number of times the experiment is performed. The output is the average of the sum of the dice values and a 6*numDice-by-1 array, where each element represents the number of experiments where the total value of the dice is equal to that element. The shape of the distribution approaches a normal distribution as the number of rolls increases. The function also uses the randi command in Matlab and the bar function to plot the histogram.
gfd43tg
Gold Member

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

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

## 1. What is the probability of rolling a specific number on a single die?

The probability of rolling a specific number on a single die is 1/6, or approximately 16.67%. This is because there are 6 possible outcomes when rolling a die (1, 2, 3, 4, 5, 6), and each outcome has an equal chance of occurring.

## 2. How do you calculate the probability of rolling a certain sum on multiple dice?

To calculate the probability of rolling a certain sum on multiple dice, you can use the formula (n/6)^x, where n is the number of dice and x is the desired sum. For example, if you want to find the probability of rolling a sum of 9 with 3 dice, the formula would be (3/6)^9, which simplifies to 1/216 or approximately 0.46%.

## 3. Can you use matlab to simulate dice rolls and calculate probabilities?

Yes, matlab has built-in functions such as randi() that can simulate dice rolls and calculate probabilities. Additionally, you can create your own custom functions to perform more complex simulations and calculations.

## 4. What is the difference between theoretical probability and experimental probability in dice rolling?

Theoretical probability is based on mathematical calculations and assumes that all outcomes are equally likely. Experimental probability, on the other hand, is based on actual data from repeated trials and may differ from theoretical probability due to chance or other factors.

## 5. How can you use the results of dice rolling simulations to make predictions?

By running multiple simulations and analyzing the data, you can determine the likelihood of certain outcomes and use this information to make predictions. For example, if you roll a die 100 times and record the results, you may find that each number appears approximately 16.67% of the time. This can be used to predict the probability of rolling a specific number in future trials.

• Programming and Computer Science
Replies
1
Views
3K
• Programming and Computer Science
Replies
1
Views
3K
• Set Theory, Logic, Probability, Statistics
Replies
6
Views
1K
• Precalculus Mathematics Homework Help
Replies
53
Views
6K
• Engineering and Comp Sci Homework Help
Replies
8
Views
1K
• Set Theory, Logic, Probability, Statistics
Replies
16
Views
2K
• Set Theory, Logic, Probability, Statistics
Replies
4
Views
945
• Set Theory, Logic, Probability, Statistics
Replies
41
Views
4K
• Precalculus Mathematics Homework Help
Replies
11
Views
2K
• Precalculus Mathematics Homework Help
Replies
14
Views
4K