MATLAB Answers

0

Rolling all six numbers on a six sided die

cody madsen さんによって質問されました 2019 年 11 月 13 日 5:44
最新アクティビティ Rik
さんによって コメントされました 2019 年 11 月 13 日 10:25
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 件のコメント

Cody's Comment shifted here
If this works I am not allowed to use some of those commands as we were not taught them. for example I cant use the sum function, I have also not seen the ismember function.
@RiK my bad, totally forgot
James Tursa
on 13 Nov 2019 at 8:10
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.

サインイン to comment.

2 件の回答

Rik
Answer by Rik
on 13 Nov 2019 at 8:14

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

  1 件のコメント

サインイン to comment.


James Tursa
Answer by James Tursa
on 13 Nov 2019 at 9:14
Edited by James Tursa
on 13 Nov 2019 at 9:22

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
on 13 Nov 2019 at 9:37
Then spell it out
while v(1)~=w(1) || v(2)~=w(2) || ... etc.
Rik
on 13 Nov 2019 at 10:25
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?

サインイン to comment.



Translated by