Rolling all six numbers on a six sided die

2 次查看(过去 30 天)
If I were to roll a 6 sided die and wanted to roll each number 1 time how many times on average would it take for me to roll each number? There is replacement of each number. I also have to do this for n trial and return the average number of rolls.
I am not sure of all of the functions I am not allowed to use since I don't know all of the functions available within matlab but from what I have learned I am to use for - loops, while - loops, randi function, and if statements. We have only learned very basic functions so far so I have not been exposed to sum functions, ismember functions, or all functions. The code won't be the most efficient that matlab is capable of but will be able to complete the task using very beginner methods.
  7 个评论
James Tursa
James Tursa 2019-11-13
编辑:James Tursa 2019-11-13
If you don't know how to write the code, it is often useful to take a step back and simply write an algorithm in English for how you would do it. Then try to turn the English into code. For example, an outline in English based on some of your ideas might look something like this:
n = an input that is the number of trials to do
% start a loop here for the number of trials, runs n times
vector = zeros(1,6); % a six element vector of zeros to keep track of the numbers that were rolled in this trial
x = 0; % the total number of rolls for this trial
% while we haven't rolled all six numbers yet for this trial
k = randi(6); % make another roll
% record this roll in the appropriate vector spot
x = x + 1; % increment the number of rolls for this trial
% end the while rolling loop
% remember the number of rolls it took for this trial and then do the next trial
% when done with all the trials, calculate the average number of rolls it took
So, look at this outline and see if you can turn the lines into code.

请先登录,再进行评论。

回答(2 个)

Rik
Rik 2019-11-13
At first I mis-interpreted the question, so that may have caused some confusion.
What you can do is create a logical vector with 6 elements. Then in your while loop you can use the dice throw as the index. How could you then use the all function to check if every number has been rolled? The code below has some gaps for you to fill.
HasBeenRolled=false(1,6);
%initialize loop variables
n_rolls=0;
cond=true;
%start loop
while cond
roll=randi(6);
n_rolls= _____
%something with HasBeenRolled
cond= ____ %use the all function here
end

James Tursa
James Tursa 2019-11-13
编辑:James Tursa 2019-11-13
This:
v = zeros(1,NToys);
NRolls = 0;
needs to be inside your Trial loop so that it resets for each trial.
A simpler way to create your w:
w = 1:NToys;
Your while loop condition is that if any v is not equal to its w counterpart, so
while any(v ~= w)
v~=w is a vector result and is not doing what you expect for the conditional test. Or you could have used:
while ~isequal(v,w)
And after your while loop is over, you need to remember the number of rolls it took for this trial. E.g.,
NRolls_trial(Trial) = Nrolls;
Then you can average them when it is all over.
  2 个评论
James Tursa
James Tursa 2019-11-13
Then spell it out
while v(1)~=w(1) || v(2)~=w(2) || ... etc.
Rik
Rik 2019-11-13
Why can't you use function you haven't learned? That doesn't make sense. The whole point of Matlab is that you have a large library at your disposal. For some homework I get it: if the assignment is to write a function that sorts a vector, then using sort is obviously not allowed, but why would any() not be allowed?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by