rolling a dice 1000 times
2 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
回答(1 个)
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
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!