To roll a dice simulator

I'm trying to create a dice simulator using MATLAB. I have to use the function input and output to create a loop. We let K be the integer from 1 to 6 that represents the number we want to see from the die. We let n be the integer that represent the number of rolls for the first K to appear from the die. We can use the randi function.
This is what I have so far:
numberOfExperiments = 6; %I know that is our n or the number of rolls%
numberOfDice = 1;
rollValues = randi(6, [numberOfDice, numberOfExperiments])
I'm trying to connect that with this:
function [n] = firstRollK(K)
% code
end

 Accepted Answer

Star Strider
Star Strider on 27 Jan 2018

0 votes

One way to approach this (and the one that seems most compatible with your homework assignment) is to use a while loop with an internal counter ‘n’ that keeps rolling the die (and increments) until ‘k’ appears. The loop then stops and reports ‘n’.

17 Comments

Oh my gosh, yes! That's what I need. So I'm thinking:
function [n] = firstRollK(K)
while true
n=randi(6)
if n==K
break
end
end
end
I'm not sure. I'm just very frustrated because I know what's going on, but I can't put it to code. Thank you very much for your help.
You missed the counting part.
Oh. Um, what is that exactly? The 'counter' is what you're saying? And, I guess where would that go?
I was thinking of something like this:
function k = firstRollK(K)
k = 0;
n = 0;
while n ~= K
k = k + 1;
n = randi(6);
end
end
Then call it as:
Kfirst = firstRollK(5)
Here, ‘k’ is the counter, and ‘n’ is the result you’re testing.
It worked in my experiments.
Don’t worry about being frustrated about not being able to code your thoughts — it’s still something I deal with!
Okay, so this:
Kfirst = firstRollK(5)
function k = firstRollK(K)
k = 0;
n = 0;
while n ~= K
k = k + 1;
n = randi(6);
end
end
I have a few questions. I ran it, and it keeps giving me the answer '5.' I'm guessing that's not supposed to happen. I don't understand why 'k' and 'n' are being told to be 0.
And thank you for being so understanding. It really means a lot that you're helping.
My pleasure.
When I ran it, it didn’t repeatedly give 5 as the answer. It gave anything from 1 to 14.
If you are using R2016a or earlier, check to be certain that you have saved it as firstRollK.m, and then have called it correctly.
In R2016b or later allow functions in script files. However, functions must be at the end of the file. See the relevant documentation for details.
I cleared MATLAB in hopes of maybe fixing it, and I think I just made it worse. I ran the code as it was above, and now, it's giving me 'error' results. Thank you again for your help.
My pleasure.
See my previous Comment, since I was likely typing it at the same time you were typing yours.
Okay so I ran the file that looks like this:
function [k] = firstRollK(K)
k=0;
n=0;
while n ~= K
k = k+1;
n = randi(6);
end
Kfirst = firstRollK(5)
end
Right? I ran that, and it says: "Error using firstRollK (line 5) Not enough input arguments."
I'm using the 2015a version.
Not quite right.
You have to save:
function k = firstRollK(K)
k = 0;
n = 0;
while n ~= K
k = k + 1;
n = randi(6);
end
end
as: firstRollK.m.
Then call it from your script as:
Kfirst = firstRollK(5)
Run it several times. You will get different values for ‘Kfirst’ in different calls to it.
Ohhhhh okay. I got it! It worked. Yay! I'm trying to limit it to only numbers 1-6, and I think randi(6) did that. I don't understand why I'm getting 14.
Great!
You are getting 14 because it took 14 ‘tosses’ of the die before it came up 5 (or whatever number you wanted it to produce). This is to be expected, and is likely the purpose of the assignment.
The probability of the one die coming up with any particular number on any one ‘roll’ is (1/6).
14 rolls
Oh my gosh. Okay. That makes so much sense. But quick question. Why k and n equal 0? I just want to be sure why on that.
And I'm going to cry of happiness. Thank you so so much. To the both of you. It means a lot.
As always, my pleasure.
It’s necessary to initialize ‘n’ to 0 (or any number other than ‘K’) because the while loop tests for it. Setting ‘k’ to 0 is necessary because it counts the iterations (dice rolls) necessary for ‘n’ to equal ‘K’, and increments in the loop.
I apologize for the delay in responding. It was bedtime for me here. Morning now.
Thank you very much!
As always, my pleasure!
Have fun!

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 27 Jan 2018

0 votes

For work like this, you can proceed in two basic ways:
1) roll the dice one at a time until you get the target number, counting rolls as you go. Repeat this for the number of experiments.
2) roll as many dice as there are experiments, some arbitrary number of times each (e.g.,50.) Then with that data array on hand, for each column, head down until you reach the target number, counting as you go. Note though that unless you take further steps, this is not as robust, because you might not happen to see any occurrences of the target number with the arbitrary number of rolls. There are tricks to vectorize the counting.
In a quick simulation I did with 100000 experiments, the maximum number of rolls to reach "3" was 70.

Categories

Find more on Optimization in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!