Rolling all six numbers on a six sided die
2 views (last 30 days)
Show older comments
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 Comments
James Tursa
on 13 Nov 2019
Edited: James Tursa
on 13 Nov 2019
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.
Answers (2)
Rik
on 13 Nov 2019
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
on 13 Nov 2019
Edited: James Tursa
on 13 Nov 2019
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 Comments
James Tursa
on 13 Nov 2019
Then spell it out
while v(1)~=w(1) || v(2)~=w(2) || ... etc.
Rik
on 13 Nov 2019
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?
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!