フィルターのクリア

rolling a dice 1000 times

1 回表示 (過去 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
  1 件のコメント
Shannu
Shannu 2020 年 11 月 7 日
thanks,man

サインインしてコメントする。

カテゴリ

Help Center および File ExchangePortfolio Optimization and Asset Allocation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by