How i can call a function 100 time?

3 views (last 30 days)
Hydro
Hydro on 11 Sep 2014
Commented: Image Analyst on 11 Sep 2014
Here is what i need to call 100 times and then record result in a vector
function [r] = dieroll(n)
r=0; % Number of time the dice been rolled
Y = 0;
% n = sum of the rolls
while Y<n;
r=r+1;
Y = Y+randi(6,1);
end
end
  1 Comment
Joseph Cheng
Joseph Cheng on 11 Sep 2014
function [r] = dieroll(n)
r=0; % Number of time the dice been rolled
Y = 0; % n = sum of the rolls
while Y<n;
r=r+1;
Y = Y+randi(6,1);
end
end
for simple reading. next time highlight the coded portion and click on the {}code button so it is easier to read. Check the preview to see that it does read well too.
Look at the documentation for while and for loops. the documentation there should be enough to get you where you need to go. what you have now is 90% there.

Sign in to comment.

Answers (3)

Rick Rosson
Rick Rosson on 11 Sep 2014
for k = 1:100
...
...
end

Image Analyst
Image Analyst on 11 Sep 2014
To call dieroll() a hundred times, try this:
r = zeros(1,100);
n = 3; % Whatever you want...
% Toss 3 dice 100 times and sum the result and store in r.
for tossNumber = 1 : 100
r(tossNumber) = dieroll(n);
end
function y = dieroll(n)
% Roll die n times and sum the result.
y = sum(randi(6, 1, n));

Joseph Cheng
Joseph Cheng on 11 Sep 2014
so in your private message you clearied by :
thanks for the comment. Here is what i tried however, its not working. I acutally need to write another function which will call the posted function 100 times. then i need to record the result and do some descriptive statistics.
function [r] = dieroll(n)
for Y = 1:10000;
r=0; % Number of time the dice been rolled
Y = 0; % n = sum of the rolls
while Y<n;
r=r+1;
Y = Y+randi(6,1);
end
end
end
so this still doesn't negate looking at the the documentation. since you need to call the dieroll function and store it in array i'll leave this here http://blogs.mathworks.com/pick/2007/08/20/matlab-basics-video/ create another function similar to dieroll
function results = Ndieroll(Ntimes)
for roll =1:Ntimes
......
end
end
by following what was given for the assignment ("create another function") you should be creating another function.
  2 Comments
Hydro
Hydro on 11 Sep 2014
Thanks Joseph, it was helpful. really appreciated
Image Analyst
Image Analyst on 11 Sep 2014
Exactly what function were you hoping to call 100 times? dieroll() (like I assumed), or randi()?

Sign in to comment.

Categories

Find more on Entering Commands 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!