Roll a die until n consecutive rolls have the same value

3 visualizaciones (últimos 30 días)
Let n be a positive integer. We plan to roll a fair 6-sided die until n consecutive rolls have the same value. My task is to write a MATLAB function that approximates the expected value EV. The routine will preform NTrials trials where a trial consists of rolling the 6-sided die until n consecutive rolls have the same value, and will then calculate EV by averaging the number of rolls over all of the trials. The program I have written so far works, however I do not know how to implement the 'n consecutive rolls' into my program.
Here is what I have so far.
function EV = ex1(n,NTrials)
%
%
rng('shuffle')
TotalNRolls = 0;
for trial = 1:NTrials
die = randi(6);
lastroll = randi(6);
TotalNRolls = TotalNRolls + 2;
while die ~= lastroll
die = lastroll;
lastroll = randi(6);
TotalNRolls = TotalNRolls + 1;
end
end
EV = TotalNRolls/NTrials;
  1 comentario
Adam Danz
Adam Danz el 19 de Oct. de 2020
" I do not know how to implement the 'n consecutive rolls' into my program. "
I would use a while loop that ends when roll n-1 matches roll n. That could be set up in many ways. Here's one way.
isMatch = false;
previousRoll = nan;
counter = 0;
while ~isMatch
counter = counter+1;
roll = randi(6);
if roll==previousRoll
isMatch = true;
continue
else
previousRoll = roll;
end
end
or maybe you want to store each round within a vector which would be simple to implement.

Iniciar sesión para comentar.

Respuesta aceptada

James Tursa
James Tursa el 19 de Oct. de 2020
Editada: James Tursa el 20 de Oct. de 2020
You made a good start, but it might help to change your inner while loop exit condition to be exactly what you are looking for ... the number of consecutive values being some number n. I.e.,
for trial = 1:NTrials
lastroll = randi(6); % just roll one die to begin
TotalNRolls = TotalNRolls + 1;
number_of_consecutive_values = 1; % just one die roll in the current streak
while( number_of_consecutive_values < n ) % keep rolling until you get n consecutive values
die = randi(6); % roll the new value
TotalNRolls = TotalNRolls + 1;
if( die == lastroll ) % we matched the value in the current streak
% do something <-- see if you can figure out what goes here
else % we did not match the value in the current streak
% do something else <-- see if you can figure out what goes here
end
end
end
EV = TotalNRolls/NTrials;
See if you can write code for the two places indicated. If die == lastroll you got a successful roll, so what would you want to happen with your number_of_consecutive_values counter? And if die ~= lastroll (the "else" part) you did not get a successful roll, so what would you want to happen with your number_of_consecutive_values counter and lastroll value in that case? Give this a try and come back if you still have problems.
  3 comentarios
James Tursa
James Tursa el 20 de Oct. de 2020
Editada: James Tursa el 20 de Oct. de 2020
No, this is not correct. Let's talk about the first part:
if die == lastroll
ConsecutiveValues = n;
If die == lastroll, that means that the current roll matched the last roll. If ConsecutiveValues happened to be 5 at the moment, that means that the current streak is 5 and gets extended by one more roll, right? So you just need to add 1 to ConsecutiveValues and then do another roll. I.e.,
if die == lastroll
ConsecutiveValues = ConsecutiveValues + 1;
Then for the "else" part when die ~= lastroll, you need to ask yourself what needs to happen. In this case you broke the current streak before it reached n and had to start a new streak, right? So you need to restart the streak from scratch. That means setting the values of ConsecutiveValues and lastroll to initial values. See if you can figure out what those should be. You are starting the new streak with just one die roll in this case.
Sophie Culhane
Sophie Culhane el 20 de Oct. de 2020
Thank you for the help, I figured it out.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre IEC Certification Kit (for ISO 26262 and IEC 61508) en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by