Roll a die until n consecutive rolls have the same value

1 次查看(过去 30 天)
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 个评论
Adam Danz
Adam Danz 2020-10-19
" 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.

请先登录,再进行评论。

采纳的回答

James Tursa
James Tursa 2020-10-19
编辑:James Tursa 2020-10-20
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 个评论
James Tursa
James Tursa 2020-10-20
编辑:James Tursa 2020-10-20
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by