Link to home
Start Free TrialLog in
Avatar of crichtx
crichtx

asked on

How do I simulate the drunken sailor with limits?

I wish to simulate the motion of a drunken sailor. Imagine a very inebriated sailor standing on the edge of a pier. The pier is X paces long and 2Y paces wide. His ship is at the end of the pier. He is located at the shore end of the pier directly in the center of the width. He starts walking toward (he thinks) the ship, but because of his condition he only has a 50% chance of moving toward the ship for each step. He has a 25% chance of moving directly left and a 25% chance of moving directly right.
A random number, r, equally distributed between 0 and 1 can be generated with the MATLAB rand function. The value of this random number will determine which way the sailor moves at each step. Remember he has a better chance of moving forward than left-right.
If during his journey the sailor’s position exceeds Y paces right or left he falls off the pier. If he exceeds X paces, he makes it safely back to the ship.
The basic algorithm is to have the sailor take a step by calling the random number generator and then incrementing the right/left/forward position of the sailor. This process is continued while the sailor is still on the pier. A single walk simulation ends when the sailor either falls off a side or reaches the ship. At this point the function will record a success or failure. The function then repeats the walk until n simulations have been executed. Then the function returns the fraction of successful walks.
I'm looking for a function sailor(X,Y,n) where the arguments are the length of the pier, the half width of the pier, and the number of times the simulation is to be repeated. The function should return the fraction of successful trips.
ASKER CERTIFIED SOLUTION
Avatar of yuk99
yuk99
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I have to mention that RANDI function that I'm using to generate random integer numbers was introduced to MATLAB only in version 2008b.
http://www.mathworks.com/help/techdoc/rn/brqyzsl-1.html#brqyzsl-3

If you have older version you will have to substitute this function. Let me know if it's the case.