what is the probability that when three dice are rolled, at least two of the dice have the same value? what is the probability that the value of the third dice roll is strictly between the values of the first two rolls? use simulation to estimate

I know for sure that I am going to use rand() to simulate the throws but how do I compute the probilities?

6 Comments

Are you asking how to simulate, or are you asking how to compute the actual probability ?
By the way: randi()
am asking how to simulate here is what i got so far(code). I dont know how am gonna compute the probability
% Probability script
% Estimates probabilities using the simulation technique
% Initializations
n = 10000;
P = 0;
% Throwing dices
x = 1 + 5*rand(n,1); % Value of the first dice
y = 1 + 5*rand(n,1); % Value of the second dice
z = 1 + 5*rand(n,1); % Value of the third dice
% Simulating throws
for k = 1:n
if x(k)== y(k)|| y(k)==z(k)
end
end
% Second condition
for k = 1:n
if x(k) < z(k) && z(k) > y(k)
end
end
You are just about there. For a simulation like this, the probability of an event is approximated by the frequency of occurrences of the event.
So for condition 1, count the number of times that your condition is satisfied (two or more dice have the same value), and divide by the number of throws (N). (Note that the number you get will be somewhere between zero and 1 as you would expect for a probability.)
should I create a variable N for the number of throws or I can use the n = 10000 that i already created as a fixed value of the number of throws? initially if i throw three dice the probability for two dice to have the same value is 3*6*5/216 = 90/216. how do I do this for multiple throws? or I am just wrong?
I meant (lowercase) n.
So to get the simulation to work try:
n = 100000;
x = randi(6,n,1); % Value of the first dice
y = randi(6,n,1); % Value of the second dice
z = randi(6,n,1); % Value of the third dice
% Simulating throws
count1 = 0; % Count the number of times condition 1 occurs
for k = 1:n
if (x(k) == y(k)) || (y(k)==z(k)) || (x(k)==z(k))
count1 = count1 + 1;
end
end
fprintf('Condition 1 occured %d times ', count1)
fprintf('Out of %d throws\n', n)
fprintf('So according to this simulation, the probability is %.3f\n', count1./n)
Try it for a small value of "n". Then try it for a larger value of "n" to get a "feel" for the computational technique.
As far as your other question "how to solve it analytically" you are close. Do you know why? What you are doing is counting all possible outcomes of throwing 3 dice and putting that number (216) in the denominator. Then figure out how many of these possible outcomes have condition 1 satisfied (and put that number in the numerator). There are lots of methods for counting large numbers (factorials, permutations and combinations) that are not intuitive, but for this (relatively) small problem, it may be worth your time to draw a "tree" diagram to (again) get a "feel" for the analytical method and circle all outcomes where two or more dice have the same value.
Hope this helps.

Sign in to comment.

Answers (3)

You made a good first effort, so I will help you out.
First, rand produces a uniform distribution, not integers, so you need to adjust your x, y, and z formulas to turn them into integers with equal probabilities:
x = floor(1 + 6*rand(n,1));
etc.
Or look at randi as Walter has suggested.
As you have coded it, this condition only covers two of the possibilities:
if x(k)== y(k)|| y(k)==z(k)
But what about if the first value matches the third value? That is not covered by your test, so you should modify it to account for this possibility.
Also, this condition you have coded isn't quite correct:
if x(k) < z(k) && z(k) > y(k)
Plug in some sample values and you will see what I mean. Remember you will need to cover the case where x(k) is less than y(k) and also the case where x(k) is greater than y(k).
As to your general question of how to calculate probabilities, you simply count the number of successes and divide that by the number of trials. E.g., start two counters, one for the first test and one for the second test, at the front of your code before you enter the loops:
P = 0;
Q = 0;
Then, inside the first if test do P = P + 1, and inside the second if test do Q = Q + 1;
At the end of your code you can then do this to get the probabilities:
P = P / n; % probability of first condition being true
Q = Q / n; % probability of second condition being true
i want add one concept, you want to compute a certain probability of dice over N times, throw N dices one time and compute, there is specified term for this concept, anyone?

3 Comments

the concept that I am trying to learn is simulations that require the use of rand() functions. I just came across a problem that asked to estimate a probability using the simulation method.
yes Mathew !, one of the assumptions of the statistical mechanics .

Sign in to comment.

Asked:

on 7 Oct 2013

Commented:

on 9 Oct 2013

Community Treasure Hunt

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

Start Hunting!