Near sighted pirate while loop?

An incredibly near-sighted pirate has just finished burying his gold on an island and now must get back to his boat at the far end of a dock. He is having a difficult time getting to his boat.
The dock is 80 feet long and 16 feet wide
Each step he takes has a 75% chance of going forward towards his ship, anddue to a peg leg a 14% chance of going directly to the right, and an 11% chance of going directly to the left. Each step = 1 foot.
Write a script that uses the rand command to randomly generate a direction for the pirate to take for each step. Does he make it to the end of the dock to find his ship or does he fall off one of the sides before he gets there? (Assume the pirate can stand right on the edge of the dock)
Run up to 1 million trials and calculate a percent chance that he makes it to his boat using the trial results. Display this percentage back to the user.
stepx=0; %At zero he's centered on the dock
%When stepx<=-5, he falls off to the left
%When stepx>=5, he falls off to the right
leftfall=0;
rightfall=0;
for i=1:10^6 %1 million trials
stepx=0;
while stepx>-5 && stepx<5
chance=rand;
if chance<=0.75
stepx=stepx-1;
else
stepx=stepx+1;
end
end
if stepx<=-5
%I know that I fell off the dock to the left
leftfall=leftfall+1;
elseif stepx>=5
%I know that I fell of the dock to the right
rightfall=rightfall+1;
end
end
disp(['The pirate fell off to the left ',num2str(leftfall),' times.'])
disp(['This was ',num2str(leftfall/(leftfall+rightfall)*100),'% of the time.'])

1 Comment

I typed this up yesterday but I'm having issues looking at this, and see ing how this answers the question. I was looking for help to find out how to get the code to calulate with the length of the dock?

Sign in to comment.

 Accepted Answer

Steven Lord
Steven Lord on 15 Apr 2020
In addition to keeping track of the pirate's position left or right of the center of the dock you need to keep track of how far from the shore he's walked. You don't have just a 1-dimensional random walk, you have a (constrained, since the pirate can't go backwards) 2-dimensional walk.
So from (0, 0) [representing the center of the dock, on the shore] the pirate has:
  • a 75% chance of going to (0, 1) [one step closer to the ship]
  • a 14% chance of going to (1, 0) [directly to the right, no closer to the ship] and
  • an 11% chance of going to (-1, 0) [directly to the left, no closer to the ship]
If you were to represent the pirate's location as a set of coordinates like this, what are the three termination conditions in terms of the coordinates?
The pirate reaches the boat if ...
The pirate falls off the right side of the dock if ...
The pirate falls off the left side of the dock if ...

14 Comments

How would I add another demention to represent someone walking down a dock? I just don't understand if it would be in the existing while loop, or if it would be in its own.
You have a variable stepx that represents how far from the center line of the dock the pirate is.
Can you think of a similar name you could use for a variable that represents how far along the dock's length the pirate is? Picture the dock:
patch([-8 8 8 -8 -8], [0 0 80 80 0], uint8([150 75 0])) % sort of wood colored
axis([-40 40 -10 100])
text(-3, -5, 'Shore')
text(-2, 85, 'Ship')
text(10, 45, 'Splash', 'Rotation', -90)
text(-10, 35, 'Splash', 'Rotation', 90)
Todd posted an answer that probably should have been a comment:
I'd probalby just call it "dockend", but I really just need the loop to tell me if the pirate makes it or not? With that I get that he'll almost always fall of the left side, but im unsure if that's because I don't acctually have an lenght of the dock in my code or not.
Why did you name your original variable stepx? Why stepx and not stepy or stepz?
One more hint: your original code is wrong in at least two ways.
chance=rand;
if chance<=0.75
stepx=stepx-1;
else
stepx=stepx+1;
end
The pirate does not have a 75% chance of stepping to the left and a 25% chance of stepping to the right which is what you've implemented. You'll need an if, an elseif, and an else in your step direction determination code since you have to detect events with 75%, 14%, and 11% chances of occurring.
if stepx<=-5
%I know that I fell off the dock to the left
leftfall=leftfall+1;
elseif stepx>=5
%I know that I fell of the dock to the right
rightfall=rightfall+1;
end
Why do you have a value of 5 on these lines? What does that represent in the context of your problem?
Rather than using the magic number 5 in your code, I'd define a variable with a descriptive name (something like dockWidth perhaps?) that you define once and use throughout your code. The descriptive name can help readability of your code. Then you might also define dockLength, probRight, etc.
The stepx variable comes from the axis on which its moving as it gets closer the the side of the dock. my orginal question was how can I add dementions, because I'm aware that the persentages are not right. I'm getting errors because I get the formatting is wrong the I just add more elseif statement.
stepx=0; %At zero he's centered on the dock
%When stepx<=-8, he falls off to the left
%When stepx>=8, he falls off to the right
leftfall=0;
rightfall=0;
for i=1:10^6 %1 million trials
stepx=0;
while stepx>-8 && stepx<8
chance=rand;
if chance<=0.75
stepx=stepx-1;
else
stepx=stepx+1;
end
end
if stepx<=-8
% He fell off the dock to the left
leftfall=leftfall+1;
elseif stepx>=8
% He fell of the dock to the right
rightfall=rightfall+1;
end
end
disp(['The pirate fell off to the left ',num2str(leftfall),' times.'])
disp(['This was ',num2str(leftfall/(leftfall+rightfall)*100),'% of the time.'])
disp(' ')
disp(['The pirate fell off the right ',num2str(rightfall),' times.'])
disp(['This was ',num2str(rightfall/(rightfall+leftfall)*100),'% of the time.'])
disp(' ')
disp('He''ll never made it. He should have plundered for some glasses.')
this is a more resent code that still has the issues.
You need something like a stepy variable to represent how far the pirate has walked along the dock, to complement your stepx variable that represents how far the pirate the pirate is left or right of the center line of the dock. At each step, either stepx (left or right) or stepy (forward) will be incremented but not both. Then check: has the pirate gone splash (either left or right, based on stepx), has he reached the ship (based on stepy), or does he keep walking?
this is how i have it in the code now
while stepx>-8 && stepx<8
chance=rand;
if chance<=0.11
stepx=stepx-1;
elseif chance<=0.14
stepx=stepx+1;
end
if chance<=0.75
step=step+1;
end
but it throws this error showing that "stepy"or "step" by its seld isnt going to work.
Error in moreBS (line 20)
step=step+1;
Your rand logic should look something like this (you should liberally use spacing and comments in your code!):
chance = rand;
if chance <= 0.11 % step to the left
stepx = stepx - 1;
elseif chance <= 0.11 + 0.14 % step to the right
stepx = stepx + 1;
else % step forward
step = step + 1;
end
The reason for the 0.11 + 0.14 is because you have already allocated the first 0.11 part of the rand to the left step. Here is how the probability is allocated for the rand value:
Also, it was stated that the pirate can stand right on the edge of the dock without falling off. So your inequality tests should include that. E.g., this
while stepx>-8 && stepx<8
should be this
while stepx >= -8 && stepx <= 8
BUT, as written the test above doesn't include the possibility that the pirate reached the end of the dock. It only includes the possibility that the pirate fell off the right or left. You need to add one more test in that while statement to see if step is a certain value (pirate reached the end of the dock).
is there any reason that i shouldnt be able use the (step = step+1;) line?
im still getting errors
What does your current code look like? As long as you initialize step before getting into the while loop, that step = step + 1 line should work just fine (unless there is something else in your code mucking with step).
that was exactly with my issue was. Thank you.
just for fun heres the results
clear
clc
stepx=0; %At zero he's centered on the dock
stepy=0;
%When stepx<=-8, he falls off to the left
%When stepx>=8, he falls off to the right
leftfall=0;
rightfall=0;
dockend=0;
for i=1:10^6 %1 million trials
stepx=0;
while stepx >= -8 && stepx <= 8
chance = rand;
if chance <= 0.11 % step to the left
stepx = stepx - 1;
elseif chance <= 0.11 + 0.14 % step to the right
stepx = stepx + 1;
elseif chance <=0.75 % step forward
stepy = stepy + 1;
end
end
if stepx<=-8
% He fell off the dock to the left
leftfall=leftfall+1;
elseif stepx>=8
% He fell of the dock to the right
rightfall=rightfall+1;
elseif stepy >= 80
% He made it to the ship
dockend = dockend+1;
end
end
disp(['The pirate fell off to the left ',num2str(leftfall),' times.'])
disp(['This was ',num2str(leftfall/(leftfall+rightfall)*100),'% of the time.'])
disp(' ')
disp(['The pirate fell off the right ',num2str(rightfall),' times.'])
disp(['This was ',num2str(rightfall/(rightfall+leftfall)*100),'% of the time.'])
disp(' ')
disp(['He made it to the boat ',num2str(dockend),' times.'])
disp('He should have plundered for some glasses.')
This
if stepx<=-8
:
elseif stepx>=8
needs to be this
if stepx < -8
:
elseif stepx > 8
Remember, the pirate can stand on the edge OK, so being exactly at -8 or 8 isn't falling off.
And you still haven't added the stepy part of the test to this:
while stepx >= -8 && stepx <= 8
I'll change that then, thanks again.

Sign in to comment.

More Answers (0)

Categories

Tags

Community Treasure Hunt

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

Start Hunting!