Asked by cody madsen
on 13 Nov 2019 at 5:44

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.

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

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.

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

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 7 Comments

## Rik (view profile)

## Direct link to this comment

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

## Steven Lord (view profile)

## Direct link to this comment

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

## Rik (view profile)

## Direct link to this comment

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

## Rik (view profile)

## Direct link to this comment

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

## KALYAN ACHARJYA (view profile)

## Direct link to this comment

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

## KALYAN ACHARJYA (view profile)

## Direct link to this comment

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

## James Tursa (view profile)

## Direct link to this comment

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

Sign in to comment.