Have a partial code but I cant get it to perform where if falls to the right.

1 view (last 30 days)
clc; clear;
stepx=0;
stepy=0;
lf=0;
rf=0;
docked=0;
trials=10^5;
for i=1:trials
stepx=0;
stepy=0;
% Looking to keep it between the stepx parameters and less that the
% stepy parameter before exiting while loop
while stepx>-9 && stepx<9 || stepy<75
chance=randi(100);
if chance<=75
stepy=stepy+1;
elseif 75>chance && chance<=89
stepx=stepx+1;
elseif 89>chance
stepx=stepx-1;
end
end
% adding up the left fall, right fall, and docked
if stepx==-9
lf=lf+1;
elseif stepx==9
rf=rf+1;
elseif stepy == 75
docked = docked+1;
end
end
disp(['The pirate fell off to the left ',num2str(lf),' times.'])
disp(['This was ',num2str(lf/(trials)*100),'% of the time.'])
disp(' ')
disp(['The pirate fell off to the right ',num2str(rf),' times.'])
disp(['This was ',num2str(rf/(trials)*100),'% of the time.'])
disp(' ')
disp(['The pirate got on board ',num2str(docked),' times.'])
disp(['This was ',num2str(docked/(trials)*100),'% of the time.'])

Answers (1)

Geoff Hayes
Geoff Hayes on 14 Apr 2022
Edited: Geoff Hayes on 14 Apr 2022
@Blain Wood - I think you need to change the first condition of
elseif 75>chance && chance<=89
to
elseif 75 < chance && chance <= 89
Also, change
elseif 89>chance
to
elseif 89 < chance
or just use an else instead.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!