Borrar filtros
Borrar filtros

rolling a dice 1000 times

2 visualizaciones (últimos 30 días)
Shannu
Shannu el 7 de Nov. de 2020
Comentada: Shannu el 7 de Nov. de 2020
iam roll a dice 1000 times. i will win if i get i get even number else loose.
how much i win in 1000 times? how much avg i win?
function [win, avg]=dice(n)
win=0;
for x=1:n
r=ceil(rand(1)*6);
if modulo(r,2)==0
win=win+1;
end
end
avg=win/n;
endfunction
for n=1000:1003
[win,avg]=dice(n);
disp("No. of wins",win)
disp("Average",avg)
end
whats wrong in my code?

Respuestas (1)

Alan Stevens
Alan Stevens el 7 de Nov. de 2020
mod not modulo. functions must come last in a script.
for n=1000:1003
[win,avg]=dice(n);
disp(['No. of wins ',int2str(win)])
disp(['Average ',num2str(avg)])
end
function [win, avg]=dice(n)
win=0;
for x=1:n
r=ceil(rand(1)*6);
if mod(r,2)==0
win=win+1;
end
end
avg=win/n;
end

Categorías

Más información sobre Portfolio Optimization and Asset Allocation en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by