rolling a dice 1000 times

2 次查看(过去 30 天)
Shannu
Shannu 2020-11-7
评论: Shannu 2020-11-7
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?

回答(1 个)

Alan Stevens
Alan Stevens 2020-11-7
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

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by