MATLAB: Drunk on the way home!

programmingprojectrandom walk

Drunk on the way home!
The drunk is halfway between home and pub, taking every step randomly in one direction. Write a function that will simulate a drunkard's movement. Its parameters will be the distance between home and pub and the number of steps to the drunken asleep (i.e. the maximum length of the simulation). The simulation ends either when the drunk arrives home or the pub, or after the number of steps has been exhausted.
Program format:
function drunkman_simulator(size,steps)
Output format:
>> drunkman_simulator(10, 100)
home . . . . . * . . . . pub
home . . . . * . . . . . pub
home . . . * . . . . . . pub
home . . . . * . . . . . pub
home . . . * . . . . . . pub
home . . . . * . . . . . pub
home . . . * . . . . . . pub
home . . * . . . . . . . pub
home . * . . . . . . . . pub
home . . * . . . . . . . pub
home . . . * . . . . . . pub
home . . * . . . . . . . pub
home . . . * . . . . . . pub
home . . * . . . . . . . pub
home . . . * . . . . . . pub
home . . . . * . . . . . pub
home . . . . . * . . . . pub
home . . . . * . . . . . pub
home . . . . . * . . . . pub
home . . . . * . . . . . pub
home . . . . . * . . . . pub
home . . . . . . * . . . pub
home . . . . . * . . . . pub
home . . . . . . * . . . pub
home . . . . . . . * . . pub
home . . . . . . . . * . pub
home . . . . . . . . . * pub
home . . . . . . . . * . pub
home . . . . . . . . . * pub
home . . . . . . . . . . pub
Arrived pub!

Best Answer

OK. I assume it works because you didn't ask a question.
When I did this back in college they also asked us to find the probability distribution function for how far away from the starting point as a function of number of steps. I think it was eye opening for most students that the expected location is not right back at the starting location, especially since you could go +1 or -1 at each step.
For what it's worth, I'm attaching my set of random walk demos, in case anyone is interested in different random walk situations.