Dice with changed probabilites
1 view (last 30 days)
Show older comments
Hello again !
I have a small problem with my code. I had to create a dice with changed probabilites for each number where the number 3 had a probability of 1/5 (20%). The task was to count the number of times i get the number 3-three times in a row.
The probability for this should be in theory 0,2*0,2*0,2=0,008=0,8%. That means i should get my three 3s every 125 throws on average. The problem now is when i throw my dice now like a 100000 times i get a probability of only 0,15% and i cant find the reason why. Is my code wrong or is my theoretic probability wrong?
I coded it like this:
clc
clear all;
lastthrow = 0;
threetimescounter = 0;
counterbacktoback = 0;
wurf = 0;
for k = 1:1000000
if rand() <= 1/5
wurf = 3;
if lastthrow == wurf
%counter nacheinander gewürfelte 3
counterbacktoback = counterbacktoback + 1; %Inkrement, bis 3 erreicht wird, dann geht ein DREIERBLOCK Counter hoch. und das letzte dreierglied wird direkt weggenommen.
if counterbacktoback == 3
threetimescounter = threetimescounter + 1;
counterbacktoback = counterbacktoback - 1; %Dekrement, weil der Letzte nichtig wird, und die ersten zwei 3er wieder auf den hypothetischen 4. 3er hoffen, der anschliessend mit den 2 letzten eine dreiergruppe darstellen koennte.
end
else
counterbacktoback = 0;
end
else
wurf = 0;
end
lastthrow = wurf;
end
pthree3s=(((1/5)*(1/5)*(1/5)))*100;
pthree3ssimul= (threetimescounter/1000000)*100;
1 Comment
Steven Lord
on 8 Dec 2020
Hint: if you want to throw three dice repeatedly and see in how many of those trials you get [3 3 3] remember that rand can generate an array of random numbers.
A = rand(4, 3)
Using this to run 1e5 trials (a two line code) I get results that are pretty consistently near 0.8%. Going up to 1e7 trials the agreement of each experiment with the theory is even better.
Accepted Answer
James Tursa
on 8 Dec 2020
Edited: James Tursa
on 8 Dec 2020
0.8% is the probability of getting three 3's in a throw of three dice. It is not the probability of the number of three 3's in a row in a large number of throws as you are computing. Also you are counting four 3's in a row as two three 3's in a row, five 3's in a row as three 3's in a row, etc. So some of those 3's get counted multiple times in your program. Bottom line is you are comparing apples to oranges with your assessment vs your computation. What is the wording of the actual assignment? What are you actually supposed to be computing? Because the actual computation you are doing does not match the 0.8% assessment.
0 Comments
More Answers (1)
Image Analyst
on 10 Dec 2020
Like James says, I don't know how you're getting 0.8%. Who told you that?
If the 3 has a 20% chance of coming up, then the others have (1-0.2)/5 = 16% chance of coming up. So I made a simple Monte Carlo experiment where I rolled a set of 3 dice three million times and found out how many times it came up all 3s. It was 2.4%.
clc; % Clear the command window.
clear all;
close all;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
fprintf('Beginning to run %s.m ...\n', mfilename);
% Chance of .16 for 1, .16 for 2, .2 for 3, .16 for 4 through 6.
h = zeros(1, 6);
all3Count = 0;
numRolls = 3000000;
for k = 1 : numRolls
r = rand(1, 3);
% Find the number for each die.
for d = 1 : length(r)
if r(d) < 0.16
die(d) = 1;
elseif r(d) < 2 * 0.16
die(d) = 2;
elseif r(d) < 2 * 0.16 + 0.2
die(d) = 3;
elseif r(d) < 2 * 0.16 + 0.2 + 0.16
die(d) = 4;
elseif r(d) < 2 * 0.16 + 0.2 + 2 * 0.16
die(d) = 5;
else
die(d) = 6;
end
% Increment if all 3 are 3
if all(die == 3)
all3Count = all3Count + 1;
end
% Create histogram
h(die(d)) = h(die(d)) + 1;
end
end
% Turn h (counts) into probability
h = h / sum(h);
bar(h);
grid on;
caption = sprintf('Histogram of %d rolls of 3 dice', numRolls);
title(caption);
xlabel('Die Number', 'FontSize', fontSize);
ylabel('Probability of that Die Number', 'FontSize', fontSize);
% Compute chance of all 3's
chance = all3Count / numRolls;
fprintf('All 3s happened %d times, which is %f percent of the %d rolls.\n', all3Count, 100*chance, numRolls);
You'll see something like:
All 3s happened 71925 times, which is 2.397500 percent of the 3000000 rolls.
and you'll see that the percentages are 20% for 3 and 16% for all the other numbers, as expected.
2 Comments
Image Analyst
on 10 Dec 2020
Edited: Image Analyst
on 10 Dec 2020
Yeah but if you roll lthe 3 dice three times, you most likely will not get all 3s ever. So that's why you need to do it thousands of times. If you just did it three times, you might conclude that the probability is zero, which is not true. The more you do it the more accurate the actual probability will be to the theoretical probability.
Your approach is different than mine. I rolled a set of 3 dice thousands of times and examined each set of three, independently of any other set of 3. Your code rolls one dice thousands of times and looks at its value on roll 1, 2, and 3, and then looks at the dice values on rolls 2, 3, and 4, and then looks at the values on rolls 3, 4, and 5, and so on. So it's not looking at independent rolls of a set of three dice. And since there is overlap, there is a correlation between your inspections, because, say, the value of the third roll in inspected in your first inspection, the second inspection, and the third inspection.
It is good though that you delved into the help and found out that randsample() had an option for specifying the weights. That's easier than the "manual" way I did it.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!