In Lab 5, you experimented with code that generated a random walk. The analogy was that of an inebriated person taking randomly-directed steps, with the resulting path being displayed on the graphics screen. The exercises for this lab also involve a random walk but with some important differences:
Once you have added the appropriate code and tested it, save a copy of the alleyWalk function in the Lab6 document. Then, make each of the following calls 5 times and report the number of steps required for each walk.
How consistent are the numbers that you obtained for each goal distance? Are the numbers more or less consistent when the goal distance is increased? Try to explain why.
As was the case with the two-dimensional random walk from Lab 5, the expected number of steps required by a one-dimensional random walk is known: on average, it takes roughly D2 steps to reach a goal distance of D from the start position. Do the numbers you obtained support this estimate? Explain.
Since the expected number of steps required by an alley walk is the square of the goal distance, it follows that if you double the goal distance, the expected number of steps goes up by a factor of 4. Do your numbers support this expectation? Explain.
As you probably noticed, there can be large variations in the numbers of steps required for random walks. For example, a sequence of nothing but heads will result in the shortest possible walk, while a sequence of alternating heads and tails will get you nowhere. If you perform only a few simulations, extreme cases such as these may occur and skew your results. By simulating a large number of random walks and averaging the results, however, rare cases such as these have minimal impact on the final average. The average number of steps taken over a large number of random walks should consistently approach the expected value.
Clearly, simulating each random walk separately and averaging the results yourself is not an attractive option when the number of desired simulations is large. Instead, you would like to automate the simulations, using a variable to keep track of the total number of steps taken by the walks, which could then be used to compute the average.
Make the following two modifications to the alleyWalk to allow for automation of the random walk simulations.
Once you have modified the alleyWalk function to return the number of steps taken by a walk, you can use the following code to perform numerous simulations and average the number of steps taken.
Does increasing the number of walks increase the consistency of the average? Should it?
Do the results of these experiments support your earlier statements regarding the consistency of the numbers when the goal distance is increased?
Do these result come close to the expected number of steps per walk (goal distance squared)? Correspondingly, does doubling the length of the walk increase the number of expected steps by a factor of 4?
Now consider a variation of the alley walk which takes place in a dead-end alley. The person starts out at the wall, and can only exit at the other end. If he is against the wall and takes a step in that direction, he merely bumps into the wall and stays where he is. At any other place in the alley, however, he is free to move in either direction as before.
Modify your alleyWalk function to match the above dead-end scenario. That is, the walker should behave exactly as before, except in the case where it is up against the wall (position 0) and flips "tails". In this case, the walker's position should remain unchanged, although this should still count as a step.
In order to test your modified function, you might find it useful to temporarily uncomment the write statement. By doing so, you can trace the movement of the walker and verify that it behaves as desired. Once you have done this, you can comment out the write statement again.
Save a copy of your modified alleyWalk definition in the Lab6 document.
Using your modified alleyWalk function, perform the same series of experiments as in Exercise 5. That is, execute the averaging code 5 times each for the following values, and list your results below:
Hand in a printout of the Lab6 document, attached to these sheets.