MATLAB Answers

Pair of dice always rolled until all results happened between (2-12) ..Expected value of throws to find

37 views (last 30 days)
Joanie Paquette
Joanie Paquette on 19 Oct 2020
Answered: Image Analyst on 19 Oct 2020
The question is :
a pair of dice should be rolled continuously until all possible outcomes have occurred at least once (2,3,...,12). Use the simulation to estimate the expected number of dice rolls needed
I tried to count but I am not able to get to it. My code is obsviously too long, but, I don't know how to do the loop while correctly(even though i read about it) and the only way I found to do this problem is to verify it through a vector zeros and ones...I think the problem is a kind of Monte Carlo. Also, I dont understand how to put an infinite number of rolls. I checked all the files related with dices but i dont find anything closer to mine. I need to simplify the code and make it work, but more importantly ... to understand what I'm doing
Here is my code :
n=
dice2 = randi(6, n, 2); % 6 faces, n fois, 2 dés
linesum = sum(dice2, 2); %sommation 2 dés par ligne
lig = 1;
col = 11;
verify = zeros(lig, col); %1 ligne, 11 colonnes
complete = ones(lig, col); %1 ligne. 11 colonnes,
count=0;
Tcount=sum(count);
while verify == complete
lig = 1;
col = 11;
verify = zeros(lig, col);
for lig = 1:1
for col = 1:11
if linesum == 2
verify(lig,col)=(lig+col)-1;
count=count+1;
elseif linesum == 3
verify(lig,col)=(lig+col)-2;
count=count+1;
elseif linesum == 4
verify(lig,col)=(lig+col)-3;
count=count+1;
elseif linesum == 5
verify(lig,col)=(lig+col)-4;
count=count+1;
elseif linesum == 6
verify(lig,col)=(lig+col)-5;
count=count+1;
elseif linesum == 7
verify(lig,col)=(lig+col)-6;
count=count+1;
elseif linesum == 8
verify(lig,col)=(lig+col)-7;
count=count+1;
elseif linesum == 9
verify(lig,col)=(lig+col)-8;
count=count+1;
elseif linesum == 10
verify(lig,col)=(lig+col)-9;
count=count+1;
elseif linesum == 11
verify(lig,col)=(lig+col)-10;
count=count+1;
elseif linesum == 12
verify(lig,col)=(lig+col)-11;
count=count+1;
end
end
end
end
moy=mean(count)

  0 Comments

Sign in to comment.

Answers (2)

Rik
Rik on 19 Oct 2020
Since this is homework I won't give a complete solution, but I will give hints. If you have trouble implementing these, feel free to comment.
What I would do is store the count per result in a vector (which starts out at 0). The while condidition needs to be true as long as there is any zero for the elements 2-12.
Inside the loop you can use the sum of the two dice as the index into the result vector, which will make you code much more compact an allows easy expansion to more dice.
You should not try to store an infinite number of dice rolls, you should put a single roll inside your loop.

  2 Comments

Joanie Paquette
Joanie Paquette on 19 Oct 2020
What do you mean by store the count per result in a vector ?
Is index a function of matlab? I never heard of it
For the n, i think i just need to put a high number as 100 000, i think it should be good.
Also, I read that, to store a result, you need to use the loop for ? So do i have to put a for loop before my while loop?
so count = 0
for i:n...
while...
To be honest, I dont know how to implement these, I'm sorry, can you explain a bit more?
Rik
Rik on 19 Oct 2020
You don't know how many rolls you need, so you should not use a for loop. You want to store the counts per result, so you need a vector, so you can store the counts for 2-12.
Indexing is an operation. It means you select a value from an array, so verify(lig,col) is an example of that. It selects the lig row and col column of the verify array.
I suspect you're doing a Matlab tutorial, so I suggest you go back to the teaching material. If you have specific questions I'll be happy to answer more.

Sign in to comment.


Image Analyst
Image Analyst on 19 Oct 2020
I'd do something like...
numExperiments = 2000
numRollsPerExperiment = 800;
% Instantiate array for the number of rolls it took to hit all 11 possible numbers.
numRolls = zeros(1, numExperiments);
% Run the 2000 experiments.
for k = 1 : numExperiments
% For each experiment, get random rolls.
rolls = randi(6, numRollsPerExperiment, 2);
% Sum the two die values for all 800 rolls.
s = sum(rolls, 2);
% Initialize counter for the possible sums.
hist = zeros(1, 12);
% See how many rolls it takes until slots 2-12 are 1, meaning every sum has been obtained.
for r = 1 : size(rolls, 1)
% Code for you to do...
end
end
% Done. Let's do some fancy plotting just for fun.
subplot(2, 1, 1);
bar(numRolls);
title('Number of rolls it took for each experiment');
xlabel('Roll number');
grid on;
subplot(2, 1, 2);
histogram(numRolls);
xlabel('rolls');
ylabel('Count')
grid on;
% Report the mean number of rolls
meanNumberOfRolls = mean(numRolls)
title('Histogram');
Hopefully I haven't given too much away.

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!