Borrar filtros
Borrar filtros

How i can call a function 100 time?

5 visualizaciones (últimos 30 días)
Hydro
Hydro el 11 de Sept. de 2014
Comentada: Image Analyst el 11 de Sept. de 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 comentario
Joseph Cheng
Joseph Cheng el 11 de Sept. de 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.

Iniciar sesión para comentar.

Respuestas (3)

Rick Rosson
Rick Rosson el 11 de Sept. de 2014
for k = 1:100
...
...
end

Image Analyst
Image Analyst el 11 de Sept. de 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 el 11 de Sept. de 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 comentarios
Hydro
Hydro el 11 de Sept. de 2014
Thanks Joseph, it was helpful. really appreciated
Image Analyst
Image Analyst el 11 de Sept. de 2014
Exactly what function were you hoping to call 100 times? dieroll() (like I assumed), or randi()?

Iniciar sesión para comentar.

Categorías

Más información sobre Entering Commands en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by