rolling a dice 1000 times

2 visualizzazioni (ultimi 30 giorni)
Shannu
Shannu il 7 Nov 2020
Commentato: Shannu il 7 Nov 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?

Risposte (1)

Alan Stevens
Alan Stevens il 7 Nov 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

Categorie

Scopri di più su MATLAB in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by