Drunk on the way home!

3 次查看(过去 30 天)
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!
  10 个评论
Ing
Ing 2020-5-10
I runned the code but It doesn't work. I couldn't understand why, because I didn't change anything from the atteched m file. It has to be working as I understand.
Image Analyst
Image Analyst 2020-5-10
I just copied and pasted the test3.m I attached and it runs perfectly. I think you must have changed something. Please show your error message and attach the version you actually ran.

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2020-5-3
编辑:Image Analyst 2020-5-3
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Linear Algebra 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by