A drunk man walking simulation
A drunk man stands at origin. There is a cliff at x=10 and home atx=-10. He takes steps randomly left and right. Each step has theprobability p of going left and q=1−p of going right.
We desire to simulate and plot 100 random trajectories for theinterval of 100 time instants. Once hit at home or cliff, atrajectory is stopped immediately and success or dead is declared.Otherwise, alive is declared. Obtain histograms of [success, dead,alive] for p=0.25, 0.5, and 0.75.
alive = 0
success = 0
dead = 0
p = 0.5
for (n in 1:1000)
{
i = 0
s = 0
while (s < 10 && s > -10 && i < 100)
{
x = rbinom(1, 1, p)
if(x == 1)
s = s-1
else
s = s+1
i = i+1
}
if(s == 0)
alive = alive +1
if(s == 10)
success = success + 1
if(s == -10)
dead = dead + 1
}
barplot(c(success, dead, alive))
this code not working
i need histogram and trajectories and code of mathlab