10 views (last 30 days)

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.

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.

Rik
on 13 Nov 2019

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 7 Comments

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/490789-rolling-all-six-numbers-on-a-six-sided-die#comment_766678

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/490789-rolling-all-six-numbers-on-a-six-sided-die#comment_766678

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/490789-rolling-all-six-numbers-on-a-six-sided-die#comment_766682

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/490789-rolling-all-six-numbers-on-a-six-sided-die#comment_766682

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/490789-rolling-all-six-numbers-on-a-six-sided-die#comment_766689

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/490789-rolling-all-six-numbers-on-a-six-sided-die#comment_766689

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/490789-rolling-all-six-numbers-on-a-six-sided-die#comment_766704

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/490789-rolling-all-six-numbers-on-a-six-sided-die#comment_766704

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/490789-rolling-all-six-numbers-on-a-six-sided-die#comment_766710

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/490789-rolling-all-six-numbers-on-a-six-sided-die#comment_766710

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/490789-rolling-all-six-numbers-on-a-six-sided-die#comment_766713

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/490789-rolling-all-six-numbers-on-a-six-sided-die#comment_766713

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/490789-rolling-all-six-numbers-on-a-six-sided-die#comment_766734

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/490789-rolling-all-six-numbers-on-a-six-sided-die#comment_766734

Sign in to comment.